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++98, c++03, c++11, c++14 10 11 // XFAIL: dylib-has-no-bad_any_cast && !no-exceptions 12 13 // <any> 14 15 // template <class ValueType> 16 // any& operator=(ValueType&&); 17 18 // Test value copy and move assignment. 19 20 #include <any> 21 #include <cassert> 22 23 #include "any_helpers.h" 24 #include "count_new.h" 25 #include "test_macros.h" 26 27 using std::any; 28 using std::any_cast; 29 30 template <class LHS, class RHS> 31 void test_assign_value() { 32 assert(LHS::count == 0); 33 assert(RHS::count == 0); 34 LHS::reset(); 35 RHS::reset(); 36 { 37 any lhs(LHS(1)); 38 any const rhs(RHS(2)); 39 40 assert(LHS::count == 1); 41 assert(RHS::count == 1); 42 assert(RHS::copied == 0); 43 44 lhs = rhs; 45 46 assert(RHS::copied == 1); 47 assert(LHS::count == 0); 48 assert(RHS::count == 2); 49 50 assertContains<RHS>(lhs, 2); 51 assertContains<RHS>(rhs, 2); 52 } 53 assert(LHS::count == 0); 54 assert(RHS::count == 0); 55 LHS::reset(); 56 RHS::reset(); 57 { 58 any lhs(LHS(1)); 59 any rhs(RHS(2)); 60 61 assert(LHS::count == 1); 62 assert(RHS::count == 1); 63 assert(RHS::moved == 1); 64 65 lhs = std::move(rhs); 66 67 assert(RHS::moved >= 1); 68 assert(RHS::copied == 0); 69 assert(LHS::count == 0); 70 assert(RHS::count == 1 + rhs.has_value()); 71 LIBCPP_ASSERT(!rhs.has_value()); 72 73 assertContains<RHS>(lhs, 2); 74 if (rhs.has_value()) 75 assertContains<RHS>(rhs, 0); 76 } 77 assert(LHS::count == 0); 78 assert(RHS::count == 0); 79 } 80 81 template <class RHS> 82 void test_assign_value_empty() { 83 assert(RHS::count == 0); 84 RHS::reset(); 85 { 86 any lhs; 87 RHS rhs(42); 88 assert(RHS::count == 1); 89 assert(RHS::copied == 0); 90 91 lhs = rhs; 92 93 assert(RHS::count == 2); 94 assert(RHS::copied == 1); 95 assert(RHS::moved >= 0); 96 assertContains<RHS>(lhs, 42); 97 } 98 assert(RHS::count == 0); 99 RHS::reset(); 100 { 101 any lhs; 102 RHS rhs(42); 103 assert(RHS::count == 1); 104 assert(RHS::moved == 0); 105 106 lhs = std::move(rhs); 107 108 assert(RHS::count == 2); 109 assert(RHS::copied == 0); 110 assert(RHS::moved >= 1); 111 assertContains<RHS>(lhs, 42); 112 } 113 assert(RHS::count == 0); 114 RHS::reset(); 115 } 116 117 118 template <class Tp, bool Move = false> 119 void test_assign_throws() { 120 #if !defined(TEST_HAS_NO_EXCEPTIONS) 121 auto try_throw = 122 [](any& lhs, Tp& rhs) { 123 try { 124 Move ? lhs = std::move(rhs) 125 : lhs = rhs; 126 assert(false); 127 } catch (my_any_exception const &) { 128 // do nothing 129 } catch (...) { 130 assert(false); 131 } 132 }; 133 // const lvalue to empty 134 { 135 any lhs; 136 Tp rhs(1); 137 assert(Tp::count == 1); 138 139 try_throw(lhs, rhs); 140 141 assert(Tp::count == 1); 142 assertEmpty<Tp>(lhs); 143 } 144 { 145 any lhs((small(2))); 146 Tp rhs(1); 147 assert(small::count == 1); 148 assert(Tp::count == 1); 149 150 try_throw(lhs, rhs); 151 152 assert(small::count == 1); 153 assert(Tp::count == 1); 154 assertContains<small>(lhs, 2); 155 } 156 { 157 any lhs((large(2))); 158 Tp rhs(1); 159 assert(large::count == 1); 160 assert(Tp::count == 1); 161 162 try_throw(lhs, rhs); 163 164 assert(large::count == 1); 165 assert(Tp::count == 1); 166 assertContains<large>(lhs, 2); 167 } 168 #endif 169 } 170 171 172 // Test that any& operator=(ValueType&&) is *never* selected for: 173 // * std::in_place type. 174 // * Non-copyable types 175 void test_sfinae_constraints() { 176 { // Only the constructors are required to SFINAE on in_place_t 177 using Tag = std::in_place_type_t<int>; 178 using RawTag = std::remove_reference_t<Tag>; 179 static_assert(std::is_assignable<std::any, RawTag&&>::value, ""); 180 } 181 { 182 struct Dummy { Dummy() = delete; }; 183 using T = std::in_place_type_t<Dummy>; 184 static_assert(std::is_assignable<std::any, T>::value, ""); 185 } 186 { 187 // Test that the ValueType&& constructor SFINAE's away when the 188 // argument is non-copyable 189 struct NoCopy { 190 NoCopy() = default; 191 NoCopy(NoCopy const&) = delete; 192 NoCopy(NoCopy&&) = default; 193 }; 194 static_assert(!std::is_assignable<std::any, NoCopy>::value, ""); 195 static_assert(!std::is_assignable<std::any, NoCopy&>::value, ""); 196 } 197 } 198 199 int main(int, char**) { 200 test_assign_value<small1, small2>(); 201 test_assign_value<large1, large2>(); 202 test_assign_value<small, large>(); 203 test_assign_value<large, small>(); 204 test_assign_value_empty<small>(); 205 test_assign_value_empty<large>(); 206 test_assign_throws<small_throws_on_copy>(); 207 test_assign_throws<large_throws_on_copy>(); 208 test_assign_throws<throws_on_move, /* Move = */ true>(); 209 test_sfinae_constraints(); 210 211 return 0; 212 } 213