[libcxx-commits] [libcxx] [libc++][any][NFC] Reformat and refactor any_cast tests (PR #169057)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Nov 21 08:10:42 PST 2025
https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/169057
...in preparation for https://github.com/llvm/llvm-project/pull/168826 as requested in the review.
>From 55322cc78a251e3b9e0bb2452813d98f8cb73ebb Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Fri, 21 Nov 2025 18:09:46 +0200
Subject: [PATCH] Reformat and refactor any_cast tests
...in preparation for https://github.com/llvm/llvm-project/pull/168826 as requested in the review.
---
.../any.cast/any_cast_pointer.pass.cpp | 237 ++++-----
.../any.cast/any_cast_reference.pass.cpp | 483 +++++++++---------
..._request_invalid_value_category.verify.cpp | 62 +--
.../any.cast/const_correctness.verify.cpp | 20 +-
.../not_copy_constructible.verify.cpp | 30 +-
.../any.cast/reference_types.verify.cpp | 41 +-
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 5143befffce73..a5f0e8b80ebec 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 079be66771944..4ba9bf6f30823 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 34f785fdff6bc..c9800c5a01431 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 at any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- // expected-error at any:* {{drops 'const' qualifier}}
- std::any_cast<TestType &>(a); // expected-note {{requested here}}
-
- const std::any a2(42);
- // expected-error-re at any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
- // expected-error at 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 at 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 at 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 at any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ // expected-error at any:* {{drops 'const' qualifier}}
+ std::any_cast<TestType&>(a); // expected-note {{requested here}}
+
+ const std::any a2(42);
+ // expected-error-re at any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
+ // expected-error at 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 at any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
- // expected-error at 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 at 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 at any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
- // expected-error at 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 at 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 at any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
+ // expected-error at 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 at any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
+ // expected-error at 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 1830626d7f410..a60bb1bbc8709 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 at 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 at 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 at 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 at 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 at 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 at 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 at 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 at 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 3b9aac581a083..8b25d5ee520b7 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 at 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 at 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 at 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 at 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 at 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 at 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 0a13fdf0c6d80..a055dcbf7f39c 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 at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &>(&a); // expected-note {{requested here}}
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&>(&a); // expected-note {{requested here}}
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &&>(&a); // expected-note {{requested here}}
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&&>(&a); // expected-note {{requested here}}
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const &>(&a); // expected-note {{requested here}}
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&>(&a); // expected-note {{requested here}}
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const&&>(&a); // expected-note {{requested here}}
+ // expected-error-re at 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 at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &>(&a2); // expected-note {{requested here}}
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&>(&a2); // expected-note {{requested here}}
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int &&>(&a2); // expected-note {{requested here}}
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int&&>(&a2); // expected-note {{requested here}}
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- std::any_cast<int const &>(&a2); // expected-note {{requested here}}
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&>(&a2); // expected-note {{requested here}}
- // expected-error-re at 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 at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ std::any_cast<int const&&>(&a2); // expected-note {{requested here}}
}
More information about the libcxx-commits
mailing list