1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 // XFAIL: availability=macosx10.13
13 // XFAIL: availability=macosx10.12
14 // XFAIL: availability=macosx10.11
15 // XFAIL: availability=macosx10.10
16 // XFAIL: availability=macosx10.9
17 // XFAIL: availability=macosx10.8
18 // XFAIL: availability=macosx10.7
22 // template <class T, class ...Args> any make_any(Args&&...);
23 // template <class T, class U, class ...Args>
24 // any make_any(initializer_list<U>, Args&&...);
29 #include "any_helpers.h"
30 #include "count_new.hpp"
31 #include "test_macros.h"
38 void test_make_any_type() {
39 // constructing from a small type should perform no allocations.
40 DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
41 assert(Type::count == 0);
44 any a = std::make_any<Type>();
46 assert(Type::count == 1);
47 assert(Type::copied == 0);
48 assert(Type::moved == 0);
49 assertContains<Type>(a, 0);
51 assert(Type::count == 0);
54 any a = std::make_any<Type>(101);
56 assert(Type::count == 1);
57 assert(Type::copied == 0);
58 assert(Type::moved == 0);
59 assertContains<Type>(a, 101);
61 assert(Type::count == 0);
64 any a = std::make_any<Type>(-1, 42, -1);
66 assert(Type::count == 1);
67 assert(Type::copied == 0);
68 assert(Type::moved == 0);
69 assertContains<Type>(a, 42);
71 assert(Type::count == 0);
76 void test_make_any_type_tracked() {
77 // constructing from a small type should perform no allocations.
78 DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
80 any a = std::make_any<Type>();
81 assertArgsMatch<Type>(a);
84 any a = std::make_any<Type>(-1, 42, -1);
85 assertArgsMatch<Type, int, int, int>(a);
87 // initializer_list constructor tests
89 any a = std::make_any<Type>({-1, 42, -1});
90 assertArgsMatch<Type, std::initializer_list<int>>(a);
94 any a = std::make_any<Type>({-1, 42, -1}, x);
95 assertArgsMatch<Type, std::initializer_list<int>, int&>(a);
99 #ifndef TEST_HAS_NO_EXCEPTIONS
102 SmallThrows(int) { throw 42; }
103 SmallThrows(std::initializer_list<int>, int) { throw 42; }
105 static_assert(IsSmallObject<SmallThrows>::value, "");
108 LargeThrows(int) { throw 42; }
109 LargeThrows(std::initializer_list<int>, int) { throw 42; }
110 int data[sizeof(std::any)];
112 static_assert(!IsSmallObject<LargeThrows>::value, "");
114 template <class Type>
115 void test_make_any_throws()
119 TEST_IGNORE_NODISCARD std::make_any<Type>(101);
121 } catch (int const&) {
126 TEST_IGNORE_NODISCARD std::make_any<Type>({1, 2, 3}, 101);
128 } catch (int const&) {
136 test_make_any_type<small>();
137 test_make_any_type<large>();
138 test_make_any_type<small_throws_on_copy>();
139 test_make_any_type<large_throws_on_copy>();
140 test_make_any_type<throws_on_move>();
141 test_make_any_type_tracked<small_tracked_t>();
142 test_make_any_type_tracked<large_tracked_t>();
143 #ifndef TEST_HAS_NO_EXCEPTIONS
144 test_make_any_throws<SmallThrows>();
145 test_make_any_throws<LargeThrows>();