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