1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
13 // template <class ValueType>
14 // any& operator=(ValueType&&);
16 // Test value copy and move assignment.
21 #include "any_helpers.h"
22 #include "count_new.h"
23 #include "test_macros.h"
25 template <class LHS
, class RHS
>
26 void test_assign_value() {
27 assert(LHS::count
== 0);
28 assert(RHS::count
== 0);
32 std::any lhs
= LHS(1);
33 const std::any rhs
= RHS(2);
35 assert(LHS::count
== 1);
36 assert(RHS::count
== 1);
37 assert(RHS::copied
== 0);
41 assert(RHS::copied
== 1);
42 assert(LHS::count
== 0);
43 assert(RHS::count
== 2);
45 assertContains
<RHS
>(lhs
, 2);
46 assertContains
<RHS
>(rhs
, 2);
48 assert(LHS::count
== 0);
49 assert(RHS::count
== 0);
53 std::any lhs
= LHS(1);
54 std::any rhs
= RHS(2);
56 assert(LHS::count
== 1);
57 assert(RHS::count
== 1);
58 assert(RHS::moved
== 1);
62 assert(RHS::moved
>= 1);
63 assert(RHS::copied
== 0);
64 assert(LHS::count
== 0);
65 assert(RHS::count
== 1 + rhs
.has_value());
66 LIBCPP_ASSERT(!rhs
.has_value());
68 assertContains
<RHS
>(lhs
, 2);
70 assertContains
<RHS
>(rhs
, 0);
72 assert(LHS::count
== 0);
73 assert(RHS::count
== 0);
77 void test_assign_value_empty() {
78 assert(RHS::count
== 0);
83 assert(RHS::count
== 1);
84 assert(RHS::copied
== 0);
88 assert(RHS::count
== 2);
89 assert(RHS::copied
== 1);
90 assert(RHS::moved
>= 0);
91 assertContains
<RHS
>(lhs
, 42);
93 assert(RHS::count
== 0);
98 assert(RHS::count
== 1);
99 assert(RHS::moved
== 0);
101 lhs
= std::move(rhs
);
103 assert(RHS::count
== 2);
104 assert(RHS::copied
== 0);
105 assert(RHS::moved
>= 1);
106 assertContains
<RHS
>(lhs
, 42);
108 assert(RHS::count
== 0);
113 template <class Tp
, bool Move
= false>
114 void test_assign_throws() {
115 #if !defined(TEST_HAS_NO_EXCEPTIONS)
117 [](std::any
& lhs
, Tp
& rhs
) {
119 Move
? lhs
= std::move(rhs
)
122 } catch (const my_any_exception
&) {
128 // const lvalue to empty
132 assert(Tp::count
== 1);
136 assert(Tp::count
== 1);
137 assertEmpty
<Tp
>(lhs
);
140 std::any lhs
= small(2);
142 assert(small::count
== 1);
143 assert(Tp::count
== 1);
147 assert(small::count
== 1);
148 assert(Tp::count
== 1);
149 assertContains
<small
>(lhs
, 2);
152 std::any lhs
= large(2);
154 assert(large::count
== 1);
155 assert(Tp::count
== 1);
159 assert(large::count
== 1);
160 assert(Tp::count
== 1);
161 assertContains
<large
>(lhs
, 2);
167 // Test that any& operator=(ValueType&&) is *never* selected for:
168 // * std::in_place type.
169 // * Non-copyable types
170 void test_sfinae_constraints() {
171 { // Only the constructors are required to SFINAE on in_place_t
172 using Tag
= std::in_place_type_t
<int>;
173 using RawTag
= std::remove_reference_t
<Tag
>;
174 static_assert(std::is_assignable
<std::any
, RawTag
&&>::value
, "");
177 struct Dummy
{ Dummy() = delete; };
178 using T
= std::in_place_type_t
<Dummy
>;
179 static_assert(std::is_assignable
<std::any
, T
>::value
, "");
182 // Test that the ValueType&& constructor SFINAE's away when the
183 // argument is non-copyable
186 NoCopy(NoCopy
const&) = delete;
187 NoCopy(NoCopy
&&) = default;
189 static_assert(!std::is_assignable
<std::any
, NoCopy
>::value
, "");
190 static_assert(!std::is_assignable
<std::any
, NoCopy
&>::value
, "");
194 int main(int, char**) {
195 test_assign_value
<small1
, small2
>();
196 test_assign_value
<large1
, large2
>();
197 test_assign_value
<small
, large
>();
198 test_assign_value
<large
, small
>();
199 test_assign_value_empty
<small
>();
200 test_assign_value_empty
<large
>();
201 test_assign_throws
<small_throws_on_copy
>();
202 test_assign_throws
<large_throws_on_copy
>();
203 test_assign_throws
<throws_on_move
, /* Move = */ true>();
204 test_sfinae_constraints();