xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/move_assign_noexcept.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 // basic_string& operator=(basic_string&& c)
14 //     noexcept(
15 //         allocator_traits<allocator_type>::propagate_on_container_move_assignment::value ||
16 //         allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
17 //
18 //  before C++17, we use the conforming extension
19 //     noexcept(
20 //         allocator_type::propagate_on_container_move_assignment::value &&
21 //         is_nothrow_move_assignable<allocator_type>::value); // constexpr since C++20
22 
23 #include <string>
24 #include <cassert>
25 
26 #include "test_macros.h"
27 #include "test_allocator.h"
28 
29 template <class T>
30 struct some_alloc {
31   typedef T value_type;
32   some_alloc(const some_alloc&);
33   T* allocate(std::size_t);
34 };
35 
36 template <class T>
37 struct some_alloc2 {
38   typedef T value_type;
39 
some_alloc2some_alloc240   some_alloc2() {}
41   some_alloc2(const some_alloc2&);
42   T* allocate(std::size_t);
deallocatesome_alloc243   void deallocate(void*, unsigned) {}
44 
45   typedef std::false_type propagate_on_container_move_assignment;
46   typedef std::true_type is_always_equal;
47 };
48 
49 template <class T>
50 struct some_alloc3 {
51   typedef T value_type;
52 
some_alloc3some_alloc353   some_alloc3() {}
54   some_alloc3(const some_alloc3&);
55   T* allocate(std::size_t);
deallocatesome_alloc356   void deallocate(void*, unsigned) {}
57 
58   typedef std::false_type propagate_on_container_move_assignment;
59   typedef std::false_type is_always_equal;
60 };
61 
test()62 TEST_CONSTEXPR_CXX20 bool test() {
63   {
64     typedef std::string C;
65     static_assert(std::is_nothrow_move_assignable<C>::value, "");
66   }
67   {
68     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
69     static_assert(!std::is_nothrow_move_assignable<C>::value, "");
70   }
71   {
72     typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
73 #if TEST_STD_VER > 14
74     // if the allocators are always equal, then the move assignment can be noexcept
75     static_assert(std::is_nothrow_move_assignable<C>::value, "");
76 #else
77     static_assert(!std::is_nothrow_move_assignable<C>::value, "");
78 #endif
79   }
80 #if TEST_STD_VER > 14
81   {
82     //  POCMA is false, always equal
83     typedef std::basic_string<char, std::char_traits<char>, some_alloc2<char>> C;
84     static_assert(std::is_nothrow_move_assignable<C>::value, "");
85   }
86   {
87     //  POCMA is false, not always equal
88     typedef std::basic_string<char, std::char_traits<char>, some_alloc3<char>> C;
89     static_assert(!std::is_nothrow_move_assignable<C>::value, "");
90   }
91 #endif
92 
93   return true;
94 }
95 
main(int,char **)96 int main(int, char**) {
97   test();
98 #if TEST_STD_VER > 17
99   static_assert(test());
100 #endif
101 
102   return 0;
103 }
104