diff options
Diffstat (limited to 'libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc')
-rw-r--r-- | libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc index 37a24d7653a..6508ca1e246 100644 --- a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc @@ -20,6 +20,7 @@ #include <any> #include <string> +#include <utility> #include <cstring> #include <testsuite_hooks.h> @@ -121,6 +122,54 @@ void test05() VERIFY( p == nullptr ); } +void test06() +{ + // The contained value of a std::any is always an object type, + // but std::any_cast does not forbid checking for function types. + + any a(1); + void (*p1)() = any_cast<void()>(&a); + VERIFY( p1 == nullptr ); + int (*p2)(int) = any_cast<int(int)>(&a); + VERIFY( p2 == nullptr ); + int (*p3)() = any_cast<int()>(&std::as_const(a)); + VERIFY( p3 == nullptr ); + + try { + any_cast<int(&)()>(a); + VERIFY( false ); + } catch (const std::bad_any_cast&) { + } + + try { + any_cast<int(&)()>(std::move(a)); + VERIFY( false ); + } catch (const std::bad_any_cast&) { + } + + try { + any_cast<int(&)()>(std::as_const(a)); + VERIFY( false ); + } catch (const std::bad_any_cast&) { + } +} + +void test07() +{ + int arr[3]; + any a(arr); + VERIFY( a.type() == typeid(int*) ); // contained value is decayed + + int (*p1)[3] = any_cast<int[3]>(&a); + VERIFY( a.type() != typeid(int[3]) ); // so any_cast should return nullptr + VERIFY( p1 == nullptr ); + int (*p2)[] = any_cast<int[]>(&a); + VERIFY( a.type() != typeid(int[]) ); // so any_cast should return nullptr + VERIFY( p2 == nullptr ); + const int (*p3)[] = any_cast<int[]>(&std::as_const(a)); + VERIFY( p3 == nullptr ); +} + int main() { test01(); @@ -128,4 +177,6 @@ int main() test03(); test04(); test05(); + test06(); + test07(); } |