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 && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
13
14 // <any>
15
16 // template <class ValueType>
17 // ValueType const* any_cast(any const *) noexcept;
18 //
19 // template <class ValueType>
20 // ValueType * any_cast(any *) noexcept;
21
22 #include <any>
23 #include <type_traits>
24 #include <cassert>
25
26 #include "test_macros.h"
27 #include "any_helpers.h"
28
29 // Test that the operators are properly noexcept.
test_cast_is_noexcept()30 void test_cast_is_noexcept() {
31 std::any a;
32 ASSERT_NOEXCEPT(std::any_cast<int>(&a));
33
34 const std::any& ca = a;
35 ASSERT_NOEXCEPT(std::any_cast<int>(&ca));
36 }
37
38 // Test that the return type of any_cast is correct.
test_cast_return_type()39 void test_cast_return_type() {
40 std::any a;
41 ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&a)), int*);
42 ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&a)), int const*);
43
44 const std::any& ca = a;
45 ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&ca)), int const*);
46 ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&ca)), int const*);
47 }
48
49 // Test that any_cast handles null pointers.
test_cast_nullptr()50 void test_cast_nullptr() {
51 std::any *a = nullptr;
52 assert(nullptr == std::any_cast<int>(a));
53 assert(nullptr == std::any_cast<int const>(a));
54
55 const std::any *ca = nullptr;
56 assert(nullptr == std::any_cast<int>(ca));
57 assert(nullptr == std::any_cast<int const>(ca));
58 }
59
60 // Test casting an empty object.
test_cast_empty()61 void test_cast_empty() {
62 {
63 std::any a;
64 assert(nullptr == std::any_cast<int>(&a));
65 assert(nullptr == std::any_cast<int const>(&a));
66
67 const std::any& ca = a;
68 assert(nullptr == std::any_cast<int>(&ca));
69 assert(nullptr == std::any_cast<int const>(&ca));
70 }
71 // Create as non-empty, then make empty and run test.
72 {
73 std::any a(42);
74 a.reset();
75 assert(nullptr == std::any_cast<int>(&a));
76 assert(nullptr == std::any_cast<int const>(&a));
77
78 const std::any& ca = a;
79 assert(nullptr == std::any_cast<int>(&ca));
80 assert(nullptr == std::any_cast<int const>(&ca));
81 }
82 }
83
84 template <class Type>
test_cast()85 void test_cast() {
86 assert(Type::count == 0);
87 Type::reset();
88 {
89 std::any a = Type(42);
90 const std::any& ca = a;
91 assert(Type::count == 1);
92 assert(Type::copied == 0);
93 assert(Type::moved == 1);
94
95 // Try a cast to a bad type.
96 // NOTE: Type cannot be an int.
97 assert(std::any_cast<int>(&a) == nullptr);
98 assert(std::any_cast<int const>(&a) == nullptr);
99 assert(std::any_cast<int const volatile>(&a) == nullptr);
100
101 // Try a cast to the right type, but as a pointer.
102 assert(std::any_cast<Type*>(&a) == nullptr);
103 assert(std::any_cast<Type const*>(&a) == nullptr);
104
105 // Check getting a unqualified type from a non-const any.
106 Type* v = std::any_cast<Type>(&a);
107 assert(v != nullptr);
108 assert(v->value == 42);
109
110 // change the stored value and later check for the new value.
111 v->value = 999;
112
113 // Check getting a const qualified type from a non-const any.
114 Type const* cv = std::any_cast<Type const>(&a);
115 assert(cv != nullptr);
116 assert(cv == v);
117 assert(cv->value == 999);
118
119 // Check getting a unqualified type from a const any.
120 cv = std::any_cast<Type>(&ca);
121 assert(cv != nullptr);
122 assert(cv == v);
123 assert(cv->value == 999);
124
125 // Check getting a const-qualified type from a const any.
126 cv = std::any_cast<Type const>(&ca);
127 assert(cv != nullptr);
128 assert(cv == v);
129 assert(cv->value == 999);
130
131 // Check that no more objects were created, copied or moved.
132 assert(Type::count == 1);
133 assert(Type::copied == 0);
134 assert(Type::moved == 1);
135 }
136 assert(Type::count == 0);
137 }
138
test_cast_non_copyable_type()139 void test_cast_non_copyable_type()
140 {
141 // Even though 'any' never stores non-copyable types
142 // we still need to support any_cast<NoCopy>(ptr)
143 struct NoCopy { NoCopy(NoCopy const&) = delete; };
144 std::any a(42);
145 std::any const& ca = a;
146 assert(std::any_cast<NoCopy>(&a) == nullptr);
147 assert(std::any_cast<NoCopy>(&ca) == nullptr);
148 }
149
test_cast_array()150 void test_cast_array() {
151 int arr[3];
152 std::any a(arr);
153 RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed
154 // We can't get an array out
155 int (*p)[3] = std::any_cast<int[3]>(&a);
156 assert(p == nullptr);
157 }
158
test_fn()159 void test_fn() {}
160
test_cast_function_pointer()161 void test_cast_function_pointer() {
162 using T = void(*)();
163 std::any a(test_fn);
164 // An any can never store a function type, but we should at least be able
165 // to ask.
166 assert(std::any_cast<void()>(&a) == nullptr);
167 T fn_ptr = std::any_cast<T>(a);
168 assert(fn_ptr == test_fn);
169 }
170
main(int,char **)171 int main(int, char**) {
172 test_cast_is_noexcept();
173 test_cast_return_type();
174 test_cast_nullptr();
175 test_cast_empty();
176 test_cast<small>();
177 test_cast<large>();
178 test_cast_non_copyable_type();
179 test_cast_array();
180 test_cast_function_pointer();
181
182 return 0;
183 }
184