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
14 // template <class Value> any(Value &&)
16 // Test construction from a value.
19 // 1. The value is properly move/copied depending on the value category.
20 // 2. Both small and large values are properly handled.
26 #include "any_helpers.h"
27 #include "count_new.hpp"
28 #include "test_macros.h"
34 void test_copy_value_throws()
36 #if !defined(TEST_HAS_NO_EXCEPTIONS)
37 assert(Type::count == 0);
40 assert(Type::count == 1);
44 } catch (my_any_exception const &) {
49 assert(Type::count == 1);
50 assert(t.value == 42);
52 assert(Type::count == 0);
56 void test_move_value_throws()
58 #if !defined(TEST_HAS_NO_EXCEPTIONS)
59 assert(throws_on_move::count == 0);
62 assert(throws_on_move::count == 1);
64 any const a(std::move(v));
66 } catch (my_any_exception const &) {
71 assert(throws_on_move::count == 1);
73 assert(throws_on_move::count == 0);
78 void test_copy_move_value() {
79 // constructing from a small type should perform no allocations.
80 DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
81 assert(Type::count == 0);
85 assert(Type::count == 1);
89 assert(Type::count == 2);
90 assert(Type::copied == 1);
91 assert(Type::moved == 0);
92 assertContains<Type>(a, 42);
94 assert(Type::count == 0);
98 assert(Type::count == 1);
102 assert(Type::count == 2);
103 assert(Type::copied == 0);
104 assert(Type::moved == 1);
105 assertContains<Type>(a, 42);
109 // Test that any(ValueType&&) is *never* selected for a std::in_place_type_t specialization.
110 void test_sfinae_constraints() {
111 using BadTag = std::in_place_type_t<int>;
112 using OKTag = std::in_place_t;
113 // Test that the tag type is properly handled in SFINAE
114 BadTag t = std::in_place_type<int>;
115 OKTag ot = std::in_place;
118 assertContains<int>(a, 0);
121 std::any a(std::move(t));
122 assertContains<int>(a, 0);
126 assert(containsType<OKTag>(a));
129 struct Dummy { Dummy() = delete; };
130 using T = std::in_place_type_t<Dummy>;
131 static_assert(!std::is_constructible<std::any, T>::value, "");
134 // Test that the ValueType&& constructor SFINAE's away when the
135 // argument is non-copyable
138 NoCopy(NoCopy const&) = delete;
141 static_assert(!std::is_constructible<std::any, NoCopy>::value, "");
142 static_assert(!std::is_constructible<std::any, NoCopy&>::value, "");
143 static_assert(!std::is_convertible<NoCopy, std::any>::value, "");
148 test_copy_move_value<small>();
149 test_copy_move_value<large>();
150 test_copy_value_throws<small_throws_on_copy>();
151 test_copy_value_throws<large_throws_on_copy>();
152 test_move_value_throws();
153 test_sfinae_constraints();