• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <string>
12 
13 // void swap(basic_string& c)
14 //     noexcept(!allocator_type::propagate_on_container_swap::value ||
15 //              __is_nothrow_swappable<allocator_type>::value);
16 //
17 //  In C++17, the standard says that swap shall have:
18 //     noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
19 //              allocator_traits<Allocator>::is_always_equal::value);
20 
21 // This tests a conforming extension
22 
23 #include <string>
24 #include <utility>
25 #include <cassert>
26 
27 #include "test_macros.h"
28 #include "test_allocator.h"
29 
30 template <class T>
31 struct some_alloc
32 {
33     typedef T value_type;
34 
some_allocsome_alloc35     some_alloc() {}
36     some_alloc(const some_alloc&);
37     T *allocate(size_t);
deallocatesome_alloc38     void deallocate(void*, unsigned) {}
39     typedef std::true_type propagate_on_container_swap;
40 };
41 
42 template <class T>
43 struct some_alloc2
44 {
45     typedef T value_type;
46 
some_alloc2some_alloc247     some_alloc2() {}
48     some_alloc2(const some_alloc2&);
49     T *allocate(size_t);
deallocatesome_alloc250     void deallocate(void*, unsigned) {}
51 
52     typedef std::false_type propagate_on_container_swap;
53     typedef std::true_type is_always_equal;
54 };
55 
main(int,char **)56 int main(int, char**)
57 {
58     {
59         typedef std::string C;
60         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
61     }
62 #if defined(_LIBCPP_VERSION)
63     {
64         typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
65         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
66     }
67 #endif // _LIBCPP_VERSION
68     {
69         typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
70 #if TEST_STD_VER >= 14
71     //  In C++14, if POCS is set, swapping the allocator is required not to throw
72         static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
73 #else
74         static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
75 #endif
76     }
77 #if TEST_STD_VER >= 14
78     {
79         typedef std::basic_string<char, std::char_traits<char>, some_alloc2<char>> C;
80     //  if the allocators are always equal, then the swap can be noexcept
81         static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
82     }
83 #endif
84 
85   return 0;
86 }
87