summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHristo Hristov <hghristov.rmm@gmail.com>2025-11-22 07:40:38 +0200
committerGitHub <noreply@github.com>2025-11-22 07:40:38 +0200
commit6b75b44ed5ed6e6e72955132a60ce8cca38bcbad (patch)
tree8a6c9ec0df4b23fe1172c5a55fc10a3a03e6d883
parent3841e7d818de2b7581351c6de3bfc9b13bdaeb30 (diff)
[libc++][any][NFC] Reformat and refactor any_cast tests (#169057)
...in preparation for https://github.com/llvm/llvm-project/pull/168826 as requested in the review. Co-authored-by: Hristo Hristov <zingam@outlook.com>
-rw-r--r--libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp237
-rw-r--r--libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp483
-rw-r--r--libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp62
-rw-r--r--libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp20
-rw-r--r--libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp30
-rw-r--r--libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp41
6 files changed, 428 insertions, 445 deletions
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp
index 5143befffce7..a5f0e8b80ebe 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp
@@ -25,156 +25,157 @@
// Test that the operators are properly noexcept.
void test_cast_is_noexcept() {
- std::any a;
- ASSERT_NOEXCEPT(std::any_cast<int>(&a));
+ std::any a;
+ ASSERT_NOEXCEPT(std::any_cast<int>(&a));
- const std::any& ca = a;
- ASSERT_NOEXCEPT(std::any_cast<int>(&ca));
+ const std::any& ca = a;
+ ASSERT_NOEXCEPT(std::any_cast<int>(&ca));
}
// Test that the return type of any_cast is correct.
void test_cast_return_type() {
- std::any a;
- ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&a)), int*);
- ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&a)), int const*);
+ std::any a;
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&a)), int*);
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&a)), int const*);
- const std::any& ca = a;
- ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&ca)), int const*);
- ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&ca)), int const*);
+ const std::any& ca = a;
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&ca)), int const*);
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&ca)), int const*);
}
// Test that any_cast handles null pointers.
void test_cast_nullptr() {
- std::any *a = nullptr;
- assert(nullptr == std::any_cast<int>(a));
- assert(nullptr == std::any_cast<int const>(a));
+ std::any* a = nullptr;
+ assert(nullptr == std::any_cast<int>(a));
+ assert(nullptr == std::any_cast<int const>(a));
- const std::any *ca = nullptr;
- assert(nullptr == std::any_cast<int>(ca));
- assert(nullptr == std::any_cast<int const>(ca));
+ const std::any* ca = nullptr;
+ assert(nullptr == std::any_cast<int>(ca));
+ assert(nullptr == std::any_cast<int const>(ca));
}
// Test casting an empty object.
void test_cast_empty() {
- {
- std::any a;
- assert(nullptr == std::any_cast<int>(&a));
- assert(nullptr == std::any_cast<int const>(&a));
-
- const std::any& ca = a;
- assert(nullptr == std::any_cast<int>(&ca));
- assert(nullptr == std::any_cast<int const>(&ca));
- }
- // Create as non-empty, then make empty and run test.
- {
- std::any a(42);
- a.reset();
- assert(nullptr == std::any_cast<int>(&a));
- assert(nullptr == std::any_cast<int const>(&a));
-
- const std::any& ca = a;
- assert(nullptr == std::any_cast<int>(&ca));
- assert(nullptr == std::any_cast<int const>(&ca));
- }
+ {
+ std::any a;
+ assert(nullptr == std::any_cast<int>(&a));
+ assert(nullptr == std::any_cast<int const>(&a));
+
+ const std::any& ca = a;
+ assert(nullptr == std::any_cast<int>(&ca));
+ assert(nullptr == std::any_cast<int const>(&ca));
+ }
+ // Create as non-empty, then make empty and run test.
+ {
+ std::any a(42);
+ a.reset();
+ assert(nullptr == std::any_cast<int>(&a));
+ assert(nullptr == std::any_cast<int const>(&a));
+
+ const std::any& ca = a;
+ assert(nullptr == std::any_cast<int>(&ca));
+ assert(nullptr == std::any_cast<int const>(&ca));
+ }
}
template <class Type>
void test_cast() {
- assert(Type::count == 0);
- Type::reset();
- {
- std::any a = Type(42);
- const std::any& ca = a;
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
-
- // Try a cast to a bad type.
- // NOTE: Type cannot be an int.
- assert(std::any_cast<int>(&a) == nullptr);
- assert(std::any_cast<int const>(&a) == nullptr);
- assert(std::any_cast<int const volatile>(&a) == nullptr);
-
- // Try a cast to the right type, but as a pointer.
- assert(std::any_cast<Type*>(&a) == nullptr);
- assert(std::any_cast<Type const*>(&a) == nullptr);
-
- // Check getting a unqualified type from a non-const any.
- Type* v = std::any_cast<Type>(&a);
- assert(v != nullptr);
- assert(v->value == 42);
-
- // change the stored value and later check for the new value.
- v->value = 999;
-
- // Check getting a const qualified type from a non-const any.
- Type const* cv = std::any_cast<Type const>(&a);
- assert(cv != nullptr);
- assert(cv == v);
- assert(cv->value == 999);
-
- // Check getting a unqualified type from a const any.
- cv = std::any_cast<Type>(&ca);
- assert(cv != nullptr);
- assert(cv == v);
- assert(cv->value == 999);
-
- // Check getting a const-qualified type from a const any.
- cv = std::any_cast<Type const>(&ca);
- assert(cv != nullptr);
- assert(cv == v);
- assert(cv->value == 999);
-
- // Check that no more objects were created, copied or moved.
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
- }
- assert(Type::count == 0);
+ assert(Type::count == 0);
+ Type::reset();
+ {
+ std::any a = Type(42);
+ const std::any& ca = a;
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+
+ // Try a cast to a bad type.
+ // NOTE: Type cannot be an int.
+ assert(std::any_cast<int>(&a) == nullptr);
+ assert(std::any_cast<int const>(&a) == nullptr);
+ assert(std::any_cast<int const volatile>(&a) == nullptr);
+
+ // Try a cast to the right type, but as a pointer.
+ assert(std::any_cast<Type*>(&a) == nullptr);
+ assert(std::any_cast<Type const*>(&a) == nullptr);
+
+ // Check getting a unqualified type from a non-const any.
+ Type* v = std::any_cast<Type>(&a);
+ assert(v != nullptr);
+ assert(v->value == 42);
+
+ // change the stored value and later check for the new value.
+ v->value = 999;
+
+ // Check getting a const qualified type from a non-const any.
+ Type const* cv = std::any_cast<Type const>(&a);
+ assert(cv != nullptr);
+ assert(cv == v);
+ assert(cv->value == 999);
+
+ // Check getting a unqualified type from a const any.
+ cv = std::any_cast<Type>(&ca);
+ assert(cv != nullptr);
+ assert(cv == v);
+ assert(cv->value == 999);
+
+ // Check getting a const-qualified type from a const any.
+ cv = std::any_cast<Type const>(&ca);
+ assert(cv != nullptr);
+ assert(cv == v);
+ assert(cv->value == 999);
+
+ // Check that no more objects were created, copied or moved.
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+ }
+ assert(Type::count == 0);
}
-void test_cast_non_copyable_type()
-{
- // Even though 'any' never stores non-copyable types
- // we still need to support any_cast<NoCopy>(ptr)
- struct NoCopy { NoCopy(NoCopy const&) = delete; };
- std::any a(42);
- std::any const& ca = a;
- assert(std::any_cast<NoCopy>(&a) == nullptr);
- assert(std::any_cast<NoCopy>(&ca) == nullptr);
+void test_cast_non_copyable_type() {
+ // Even though 'any' never stores non-copyable types
+ // we still need to support any_cast<NoCopy>(ptr)
+ struct NoCopy {
+ NoCopy(NoCopy const&) = delete;
+ };
+ std::any a(42);
+ std::any const& ca = a;
+ assert(std::any_cast<NoCopy>(&a) == nullptr);
+ assert(std::any_cast<NoCopy>(&ca) == nullptr);
}
void test_cast_array() {
- int arr[3];
- std::any a(arr);
- RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed
- // We can't get an array out
- int (*p)[3] = std::any_cast<int[3]>(&a);
- assert(p == nullptr);
+ int arr[3];
+ std::any a(arr);
+ RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed
+ // We can't get an array out
+ int (*p)[3] = std::any_cast<int[3]>(&a);
+ assert(p == nullptr);
}
void test_fn() {}
void test_cast_function_pointer() {
- using T = void(*)();
- std::any a(test_fn);
- // An any can never store a function type, but we should at least be able
- // to ask.
- assert(std::any_cast<void()>(&a) == nullptr);
- T fn_ptr = std::any_cast<T>(a);
- assert(fn_ptr == test_fn);
+ using T = void (*)();
+ std::any a(test_fn);
+ // An any can never store a function type, but we should at least be able
+ // to ask.
+ assert(std::any_cast<void()>(&a) == nullptr);
+ T fn_ptr = std::any_cast<T>(a);
+ assert(fn_ptr == test_fn);
}
int main(int, char**) {
- test_cast_is_noexcept();
- test_cast_return_type();
- test_cast_nullptr();
- test_cast_empty();
- test_cast<small>();
- test_cast<large>();
- test_cast_non_copyable_type();
- test_cast_array();
- test_cast_function_pointer();
+ test_cast_is_noexcept();
+ test_cast_return_type();
+ test_cast_nullptr();
+ test_cast_empty();
+ test_cast<small>();
+ test_cast<large>();
+ test_cast_non_copyable_type();
+ test_cast_array();
+ test_cast_function_pointer();
return 0;
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp
index 079be6677194..4ba9bf6f3082 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp
@@ -29,278 +29,275 @@
// Test that the operators are NOT marked noexcept.
void test_cast_is_not_noexcept() {
- std::any a;
- static_assert(!noexcept(std::any_cast<int>(static_cast<std::any&>(a))), "");
- static_assert(!noexcept(std::any_cast<int>(static_cast<std::any const&>(a))), "");
- static_assert(!noexcept(std::any_cast<int>(static_cast<std::any &&>(a))), "");
+ std::any a;
+ static_assert(!noexcept(std::any_cast<int>(static_cast<std::any&>(a))), "");
+ static_assert(!noexcept(std::any_cast<int>(static_cast<std::any const&>(a))), "");
+ static_assert(!noexcept(std::any_cast<int>(static_cast<std::any&&>(a))), "");
}
// Test that the return type of any_cast is correct.
void test_cast_return_type() {
- std::any a;
- static_assert(std::is_same<decltype(std::any_cast<int>(a)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const>(a)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&>(a)), int&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&>(a)), int const&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&&>(a)), int&&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&&>(a)), int const&&>::value, "");
-
- static_assert(std::is_same<decltype(std::any_cast<int>(std::move(a))), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const>(std::move(a))), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&>(std::move(a))), int&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&>(std::move(a))), int const&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&&>(std::move(a))), int&&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&&>(std::move(a))), int const&&>::value, "");
-
- const std::any& ca = a;
- static_assert(std::is_same<decltype(std::any_cast<int>(ca)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const>(ca)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&>(ca)), int const&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&&>(ca)), int const&&>::value, "");
+ std::any a;
+ static_assert(std::is_same<decltype(std::any_cast<int>(a)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const>(a)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&>(a)), int&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&>(a)), int const&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&&>(a)), int&&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&&>(a)), int const&&>::value, "");
+
+ static_assert(std::is_same<decltype(std::any_cast<int>(std::move(a))), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const>(std::move(a))), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&>(std::move(a))), int&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&>(std::move(a))), int const&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&&>(std::move(a))), int&&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&&>(std::move(a))), int const&&>::value, "");
+
+ const std::any& ca = a;
+ static_assert(std::is_same<decltype(std::any_cast<int>(ca)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const>(ca)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&>(ca)), int const&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&&>(ca)), int const&&>::value, "");
}
template <class Type, class ConstT = Type>
-void checkThrows(std::any& a)
-{
+void checkThrows(std::any& a) {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
- try {
- TEST_IGNORE_NODISCARD std::any_cast<Type>(a);
- assert(false);
- } catch (const std::bad_any_cast&) {
- // do nothing
- } catch (...) {
- assert(false);
- }
-
- try {
- TEST_IGNORE_NODISCARD std::any_cast<ConstT>(static_cast<const std::any&>(a));
- assert(false);
- } catch (const std::bad_any_cast&) {
- // do nothing
- } catch (...) {
- assert(false);
- }
-
- try {
- using RefType = typename std::conditional<
- std::is_lvalue_reference<Type>::value,
- typename std::remove_reference<Type>::type&&,
- Type
- >::type;
- TEST_IGNORE_NODISCARD std::any_cast<RefType>(static_cast<std::any&&>(a));
- assert(false);
- } catch (const std::bad_any_cast&) {
- // do nothing
- } catch (...) {
- assert(false);
- }
+ try {
+ TEST_IGNORE_NODISCARD std::any_cast<Type>(a);
+ assert(false);
+ } catch (const std::bad_any_cast&) {
+ // do nothing
+ } catch (...) {
+ assert(false);
+ }
+
+ try {
+ TEST_IGNORE_NODISCARD std::any_cast<ConstT>(static_cast<const std::any&>(a));
+ assert(false);
+ } catch (const std::bad_any_cast&) {
+ // do nothing
+ } catch (...) {
+ assert(false);
+ }
+
+ try {
+ using RefType = typename std::
+ conditional< std::is_lvalue_reference<Type>::value, typename std::remove_reference<Type>::type&&, Type >::type;
+ TEST_IGNORE_NODISCARD std::any_cast<RefType>(static_cast<std::any&&>(a));
+ assert(false);
+ } catch (const std::bad_any_cast&) {
+ // do nothing
+ } catch (...) {
+ assert(false);
+ }
#else
- (TEST_IGNORE_NODISCARD a);
+ (TEST_IGNORE_NODISCARD a);
#endif
}
void test_cast_empty() {
- // None of these operations should allocate.
- DisableAllocationGuard g; (TEST_IGNORE_NODISCARD g);
- std::any a;
- checkThrows<int>(a);
+ // None of these operations should allocate.
+ DisableAllocationGuard g;
+ (TEST_IGNORE_NODISCARD g);
+ std::any a;
+ checkThrows<int>(a);
}
template <class Type>
void test_cast_to_reference() {
- assert(Type::count == 0);
- Type::reset();
+ assert(Type::count == 0);
+ Type::reset();
+ {
+ std::any a = Type(42);
+ const std::any& ca = a;
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+
+ // Try a cast to a bad type.
+ // NOTE: Type cannot be an int.
+ checkThrows<int>(a);
+ checkThrows<int&, int const&>(a);
+ checkThrows<Type*, Type const*>(a);
+ checkThrows<Type const*>(a);
+
+ // Check getting a type by reference from a non-const lvalue any.
+ {
+ Type& v = std::any_cast<Type&>(a);
+ assert(v.value == 42);
+
+ Type const& cv = std::any_cast<Type const&>(a);
+ assert(&cv == &v);
+ }
+ // Check getting a type by reference from a const lvalue any.
{
- std::any a = Type(42);
- const std::any& ca = a;
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
-
- // Try a cast to a bad type.
- // NOTE: Type cannot be an int.
- checkThrows<int>(a);
- checkThrows<int&, int const&>(a);
- checkThrows<Type*, Type const*>(a);
- checkThrows<Type const*>(a);
-
- // Check getting a type by reference from a non-const lvalue any.
- {
- Type& v = std::any_cast<Type&>(a);
- assert(v.value == 42);
-
- Type const &cv = std::any_cast<Type const&>(a);
- assert(&cv == &v);
- }
- // Check getting a type by reference from a const lvalue any.
- {
- Type const& v = std::any_cast<Type const&>(ca);
- assert(v.value == 42);
-
- Type const &cv = std::any_cast<Type const&>(ca);
- assert(&cv == &v);
- }
- // Check getting a type by reference from a const rvalue any.
- {
- Type const& v = std::any_cast<Type const&>(std::move(ca));
- assert(v.value == 42);
-
- Type const &cv = std::any_cast<Type const&>(std::move(ca));
- assert(&cv == &v);
- }
- // Check getting a type by reference from a const rvalue any.
- {
- Type&& v = std::any_cast<Type&&>(std::move(a));
- assert(v.value == 42);
- assert(std::any_cast<Type&>(a).value == 42);
-
- Type&& cv = std::any_cast<Type&&>(std::move(a));
- assert(&cv == &v);
- assert(std::any_cast<Type&>(a).value == 42);
- }
- // Check getting a type by reference from a const rvalue any.
- {
- Type const&& v = std::any_cast<Type const&&>(std::move(a));
- assert(v.value == 42);
- assert(std::any_cast<Type&>(a).value == 42);
-
- Type const&& cv = std::any_cast<Type const&&>(std::move(a));
- assert(&cv == &v);
- assert(std::any_cast<Type&>(a).value == 42);
- }
- // Check that the original object hasn't been changed.
- assertContains<Type>(a, 42);
-
- // Check that no objects have been created/copied/moved.
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
+ Type const& v = std::any_cast<Type const&>(ca);
+ assert(v.value == 42);
+
+ Type const& cv = std::any_cast<Type const&>(ca);
+ assert(&cv == &v);
}
- assert(Type::count == 0);
+ // Check getting a type by reference from a const rvalue any.
+ {
+ Type const& v = std::any_cast<Type const&>(std::move(ca));
+ assert(v.value == 42);
+
+ Type const& cv = std::any_cast<Type const&>(std::move(ca));
+ assert(&cv == &v);
+ }
+ // Check getting a type by reference from a const rvalue any.
+ {
+ Type&& v = std::any_cast<Type&&>(std::move(a));
+ assert(v.value == 42);
+ assert(std::any_cast<Type&>(a).value == 42);
+
+ Type&& cv = std::any_cast<Type&&>(std::move(a));
+ assert(&cv == &v);
+ assert(std::any_cast<Type&>(a).value == 42);
+ }
+ // Check getting a type by reference from a const rvalue any.
+ {
+ Type const&& v = std::any_cast<Type const&&>(std::move(a));
+ assert(v.value == 42);
+ assert(std::any_cast<Type&>(a).value == 42);
+
+ Type const&& cv = std::any_cast<Type const&&>(std::move(a));
+ assert(&cv == &v);
+ assert(std::any_cast<Type&>(a).value == 42);
+ }
+ // Check that the original object hasn't been changed.
+ assertContains<Type>(a, 42);
+
+ // Check that no objects have been created/copied/moved.
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+ }
+ assert(Type::count == 0);
}
template <class Type>
void test_cast_to_value() {
- assert(Type::count == 0);
+ assert(Type::count == 0);
+ Type::reset();
+ {
+ std::any a = Type(42);
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+
+ // Try a cast to a bad type.
+ // NOTE: Type cannot be an int.
+ checkThrows<int>(a);
+ checkThrows<int&, int const&>(a);
+ checkThrows<Type*, Type const*>(a);
+ checkThrows<Type const*>(a);
+
+ Type::reset(); // NOTE: reset does not modify Type::count
+ // Check getting Type by value from a non-const lvalue any.
+ // This should cause the non-const copy constructor to be called.
+ {
+ Type t = std::any_cast<Type>(a);
+
+ assert(Type::count == 2);
+ assert(Type::copied == 1);
+ assert(Type::const_copied == 0);
+ assert(Type::non_const_copied == 1);
+ assert(Type::moved == 0);
+ assert(t.value == 42);
+ }
+ assert(Type::count == 1);
+ Type::reset();
+ // Check getting const Type by value from a non-const lvalue any.
+ // This should cause the const copy constructor to be called.
+ {
+ Type t = std::any_cast<Type const>(a);
+
+ assert(Type::count == 2);
+ assert(Type::copied == 1);
+ assert(Type::const_copied == 0);
+ assert(Type::non_const_copied == 1);
+ assert(Type::moved == 0);
+ assert(t.value == 42);
+ }
+ assert(Type::count == 1);
Type::reset();
+ // Check getting Type by value from a non-const lvalue any.
+ // This should cause the const copy constructor to be called.
{
- std::any a = Type(42);
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
-
- // Try a cast to a bad type.
- // NOTE: Type cannot be an int.
- checkThrows<int>(a);
- checkThrows<int&, int const&>(a);
- checkThrows<Type*, Type const*>(a);
- checkThrows<Type const*>(a);
-
- Type::reset(); // NOTE: reset does not modify Type::count
- // Check getting Type by value from a non-const lvalue any.
- // This should cause the non-const copy constructor to be called.
- {
- Type t = std::any_cast<Type>(a);
-
- assert(Type::count == 2);
- assert(Type::copied == 1);
- assert(Type::const_copied == 0);
- assert(Type::non_const_copied == 1);
- assert(Type::moved == 0);
- assert(t.value == 42);
- }
- assert(Type::count == 1);
- Type::reset();
- // Check getting const Type by value from a non-const lvalue any.
- // This should cause the const copy constructor to be called.
- {
- Type t = std::any_cast<Type const>(a);
-
- assert(Type::count == 2);
- assert(Type::copied == 1);
- assert(Type::const_copied == 0);
- assert(Type::non_const_copied == 1);
- assert(Type::moved == 0);
- assert(t.value == 42);
- }
- assert(Type::count == 1);
- Type::reset();
- // Check getting Type by value from a non-const lvalue any.
- // This should cause the const copy constructor to be called.
- {
- Type t = std::any_cast<Type>(static_cast<const std::any&>(a));
-
- assert(Type::count == 2);
- assert(Type::copied == 1);
- assert(Type::const_copied == 1);
- assert(Type::non_const_copied == 0);
- assert(Type::moved == 0);
- assert(t.value == 42);
- }
- assert(Type::count == 1);
- Type::reset();
- // Check getting Type by value from a non-const rvalue any.
- // This should cause the non-const copy constructor to be called.
- {
- Type t = std::any_cast<Type>(static_cast<std::any&&>(a));
-
- assert(Type::count == 2);
- assert(Type::moved == 1);
- assert(Type::copied == 0);
- assert(Type::const_copied == 0);
- assert(Type::non_const_copied == 0);
- assert(t.value == 42);
- assert(std::any_cast<Type&>(a).value == 0);
- std::any_cast<Type&>(a).value = 42; // reset the value
- }
- assert(Type::count == 1);
- Type::reset();
- // Check getting const Type by value from a non-const rvalue any.
- // This should cause the const copy constructor to be called.
- {
- Type t = std::any_cast<Type const>(static_cast<std::any&&>(a));
-
- assert(Type::count == 2);
- assert(Type::copied == 0);
- assert(Type::const_copied == 0);
- assert(Type::non_const_copied == 0);
- assert(Type::moved == 1);
- assert(t.value == 42);
- assert(std::any_cast<Type&>(a).value == 0);
- std::any_cast<Type&>(a).value = 42; // reset the value
- }
- assert(Type::count == 1);
- Type::reset();
- // Check getting Type by value from a const rvalue any.
- // This should cause the const copy constructor to be called.
- {
- Type t = std::any_cast<Type>(static_cast<const std::any&&>(a));
-
- assert(Type::count == 2);
- assert(Type::copied == 1);
- assert(Type::const_copied == 1);
- assert(Type::non_const_copied == 0);
- assert(Type::moved == 0);
- assert(t.value == 42);
- assert(std::any_cast<Type&>(a).value == 42);
- }
- // Ensure we still only have 1 Type object alive.
- assert(Type::count == 1);
-
- // Check that the original object hasn't been changed.
- assertContains<Type>(a, 42);
+ Type t = std::any_cast<Type>(static_cast<const std::any&>(a));
+
+ assert(Type::count == 2);
+ assert(Type::copied == 1);
+ assert(Type::const_copied == 1);
+ assert(Type::non_const_copied == 0);
+ assert(Type::moved == 0);
+ assert(t.value == 42);
}
- assert(Type::count == 0);
+ assert(Type::count == 1);
+ Type::reset();
+ // Check getting Type by value from a non-const rvalue any.
+ // This should cause the non-const copy constructor to be called.
+ {
+ Type t = std::any_cast<Type>(static_cast<std::any&&>(a));
+
+ assert(Type::count == 2);
+ assert(Type::moved == 1);
+ assert(Type::copied == 0);
+ assert(Type::const_copied == 0);
+ assert(Type::non_const_copied == 0);
+ assert(t.value == 42);
+ assert(std::any_cast<Type&>(a).value == 0);
+ std::any_cast<Type&>(a).value = 42; // reset the value
+ }
+ assert(Type::count == 1);
+ Type::reset();
+ // Check getting const Type by value from a non-const rvalue any.
+ // This should cause the const copy constructor to be called.
+ {
+ Type t = std::any_cast<Type const>(static_cast<std::any&&>(a));
+
+ assert(Type::count == 2);
+ assert(Type::copied == 0);
+ assert(Type::const_copied == 0);
+ assert(Type::non_const_copied == 0);
+ assert(Type::moved == 1);
+ assert(t.value == 42);
+ assert(std::any_cast<Type&>(a).value == 0);
+ std::any_cast<Type&>(a).value = 42; // reset the value
+ }
+ assert(Type::count == 1);
+ Type::reset();
+ // Check getting Type by value from a const rvalue any.
+ // This should cause the const copy constructor to be called.
+ {
+ Type t = std::any_cast<Type>(static_cast<const std::any&&>(a));
+
+ assert(Type::count == 2);
+ assert(Type::copied == 1);
+ assert(Type::const_copied == 1);
+ assert(Type::non_const_copied == 0);
+ assert(Type::moved == 0);
+ assert(t.value == 42);
+ assert(std::any_cast<Type&>(a).value == 42);
+ }
+ // Ensure we still only have 1 Type object alive.
+ assert(Type::count == 1);
+
+ // Check that the original object hasn't been changed.
+ assertContains<Type>(a, 42);
+ }
+ assert(Type::count == 0);
}
int main(int, char**) {
- test_cast_is_not_noexcept();
- test_cast_return_type();
- test_cast_empty();
- test_cast_to_reference<small>();
- test_cast_to_reference<large>();
- test_cast_to_value<small>();
- test_cast_to_value<large>();
+ test_cast_is_not_noexcept();
+ test_cast_return_type();
+ test_cast_empty();
+ test_cast_to_reference<small>();
+ test_cast_to_reference<large>();
+ test_cast_to_value<small>();
+ test_cast_to_value<large>();
return 0;
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp
index 34f785fdff6b..c9800c5a0143 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp
@@ -19,47 +19,35 @@
struct TestType {};
-void test_const_lvalue_cast_request_non_const_lvalue()
-{
- const std::any a;
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- // expected-error@any:* {{drops 'const' qualifier}}
- std::any_cast<TestType &>(a); // expected-note {{requested here}}
-
- const std::any a2(42);
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- // expected-error@any:* {{drops 'const' qualifier}}
- std::any_cast<int&>(a2); // expected-note {{requested here}}
-}
-
-void test_lvalue_any_cast_request_rvalue()
-{
- std::any a;
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
- std::any_cast<TestType &&>(a); // expected-note {{requested here}}
-
- std::any a2(42);
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
- std::any_cast<int&&>(a2); // expected-note {{requested here}}
+void test_const_lvalue_cast_request_non_const_lvalue() {
+ const std::any a;
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ // expected-error@any:* {{drops 'const' qualifier}}
+ std::any_cast<TestType&>(a); // expected-note {{requested here}}
+
+ const std::any a2(42);
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ // expected-error@any:* {{drops 'const' qualifier}}
+ std::any_cast<int&>(a2); // expected-note {{requested here}}
}
-void test_rvalue_any_cast_request_lvalue()
-{
- std::any a;
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
- // expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}}
- std::any_cast<TestType &>(std::move(a)); // expected-note {{requested here}}
+void test_lvalue_any_cast_request_rvalue() {
+ std::any a;
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
+ std::any_cast<TestType&&>(a); // expected-note {{requested here}}
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
- // expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}}
- std::any_cast<int&>(42);
+ std::any a2(42);
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
+ std::any_cast<int&&>(a2); // expected-note {{requested here}}
}
-int main(int, char**)
-{
- test_const_lvalue_cast_request_non_const_lvalue();
- test_lvalue_any_cast_request_rvalue();
- test_rvalue_any_cast_request_lvalue();
+void test_rvalue_any_cast_request_lvalue() {
+ std::any a;
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
+ // expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}}
+ std::any_cast<TestType&>(std::move(a)); // expected-note {{requested here}}
- return 0;
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
+ // expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}}
+ std::any_cast<int&>(42);
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp
index 1830626d7f41..a60bb1bbc870 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp
@@ -27,18 +27,18 @@
struct TestType {};
struct TestType2 {};
-void f() {
- std::any a;
+void test() {
+ std::any a;
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- std::any_cast<TestType &>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ std::any_cast<TestType&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- std::any_cast<TestType &&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ std::any_cast<TestType&&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- std::any_cast<TestType2 &>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ std::any_cast<TestType2&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- std::any_cast<TestType2 &&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ std::any_cast<TestType2&&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp
index 3b9aac581a08..8b25d5ee520b 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp
@@ -31,27 +31,27 @@
#include <any>
struct no_copy {
- no_copy() {}
- no_copy(no_copy &&) {}
- no_copy(no_copy const &) = delete;
+ no_copy() {}
+ no_copy(no_copy&&) {}
+ no_copy(no_copy const&) = delete;
};
struct no_move {
- no_move() {}
- no_move(no_move&&) = delete;
- no_move(no_move const&) {}
+ no_move() {}
+ no_move(no_move&&) = delete;
+ no_move(no_move const&) {}
};
-void f() {
- std::any a;
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
- std::any_cast<no_copy>(static_cast<std::any&>(a)); // expected-note {{requested here}}
+void test() {
+ std::any a;
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
+ std::any_cast<no_copy>(static_cast<std::any&>(a)); // expected-note {{requested here}}
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- std::any_cast<no_copy>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ std::any_cast<no_copy>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
- std::any_cast<no_copy>(static_cast<std::any &&>(a)); // OK
+ std::any_cast<no_copy>(static_cast<std::any&&>(a)); // OK
- // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
- std::any_cast<no_move>(static_cast<std::any &&>(a));
+ // expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
+ std::any_cast<no_move>(static_cast<std::any&&>(a));
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
index 0a13fdf0c6d8..a055dcbf7f39 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
@@ -18,35 +18,32 @@
#include <any>
-int main(int, char**)
-{
- std::any a = 1;
+void test() {
+ std::any a = 1;
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &>(&a); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&>(&a); // expected-note {{requested here}}
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &&>(&a); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&&>(&a); // expected-note {{requested here}}
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const &>(&a); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&>(&a); // expected-note {{requested here}}
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const&&>(&a); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&&>(&a); // expected-note {{requested here}}
- const std::any& a2 = a;
+ const std::any& a2 = a;
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &>(&a2); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&>(&a2); // expected-note {{requested here}}
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &&>(&a2); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&&>(&a2); // expected-note {{requested here}}
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const &>(&a2); // expected-note {{requested here}}
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&>(&a2); // expected-note {{requested here}}
- // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const &&>(&a2); // expected-note {{requested here}}
-
- return 0;
+ // expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&&>(&a2); // expected-note {{requested here}}
}