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 Value> any(Value &&)
16 
17 // Test construction from a value.
18 // Concerns:
19 // ---------
20 // 1. The value is properly move/copied depending on the value category.
21 // 2. Both small and large values are properly handled.
22 
23 
24 #include <any>
25 #include <cassert>
26 
27 #include "any_helpers.h"
28 #include "count_new.h"
29 #include "test_macros.h"
30 
31 using std::any;
32 using std::any_cast;
33 
34 template <class Type>
35 void test_copy_value_throws()
36 {
37 #if !defined(TEST_HAS_NO_EXCEPTIONS)
38     assert(Type::count == 0);
39     {
40         Type const t(42);
41         assert(Type::count == 1);
42         try {
43             any const a2(t);
44             assert(false);
45         } catch (my_any_exception const &) {
46             // do nothing
47         } catch (...) {
48             assert(false);
49         }
50         assert(Type::count == 1);
51         assert(t.value == 42);
52     }
53     assert(Type::count == 0);
54 #endif
55 }
56 
57 void test_move_value_throws()
58 {
59 #if !defined(TEST_HAS_NO_EXCEPTIONS)
60     assert(throws_on_move::count == 0);
61     {
62         throws_on_move v;
63         assert(throws_on_move::count == 1);
64         try {
65             any const a(std::move(v));
66             assert(false);
67         } catch (my_any_exception const &) {
68             // do nothing
69         } catch (...) {
70             assert(false);
71         }
72         assert(throws_on_move::count == 1);
73     }
74     assert(throws_on_move::count == 0);
75 #endif
76 }
77 
78 template <class Type>
79 void test_copy_move_value() {
80     // constructing from a small type should perform no allocations.
81     DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
82     assert(Type::count == 0);
83     Type::reset();
84     {
85         Type t(42);
86         assert(Type::count == 1);
87 
88         any a(t);
89 
90         assert(Type::count == 2);
91         assert(Type::copied == 1);
92         assert(Type::moved == 0);
93         assertContains<Type>(a, 42);
94     }
95     assert(Type::count == 0);
96     Type::reset();
97     {
98         Type t(42);
99         assert(Type::count == 1);
100 
101         any a(std::move(t));
102 
103         assert(Type::count == 2);
104         assert(Type::copied == 0);
105         assert(Type::moved == 1);
106         assertContains<Type>(a, 42);
107     }
108 }
109 
110 // Test that any(ValueType&&) is *never* selected for a std::in_place_type_t specialization.
111 void test_sfinae_constraints() {
112     using BadTag = std::in_place_type_t<int>;
113     using OKTag = std::in_place_t;
114     // Test that the tag type is properly handled in SFINAE
115     BadTag t = std::in_place_type<int>;
116     OKTag ot = std::in_place;
117     {
118         std::any a(t);
119         assertContains<int>(a, 0);
120     }
121     {
122         std::any a(std::move(t));
123         assertContains<int>(a, 0);
124     }
125     {
126         std::any a(ot);
127         assert(containsType<OKTag>(a));
128     }
129     {
130         struct Dummy { Dummy() = delete; };
131         using T = std::in_place_type_t<Dummy>;
132         static_assert(!std::is_constructible<std::any, T>::value, "");
133     }
134     {
135         // Test that the ValueType&& constructor SFINAE's away when the
136         // argument is non-copyable
137         struct NoCopy {
138           NoCopy() = default;
139           NoCopy(NoCopy const&) = delete;
140           NoCopy(int) {}
141         };
142         static_assert(!std::is_constructible<std::any, NoCopy>::value, "");
143         static_assert(!std::is_constructible<std::any, NoCopy&>::value, "");
144         static_assert(!std::is_convertible<NoCopy, std::any>::value, "");
145     }
146 }
147 
148 int main(int, char**) {
149     test_copy_move_value<small>();
150     test_copy_move_value<large>();
151     test_copy_value_throws<small_throws_on_copy>();
152     test_copy_value_throws<large_throws_on_copy>();
153     test_move_value_throws();
154     test_sfinae_constraints();
155 
156   return 0;
157 }
158