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