2019-04-24 Jonathan Wakely <jwakely@redhat.com>
+ PR libstdc++/90220 (partial)
+ * include/std/any (any_cast<T>(any*), any_cast<T>(const any*)): Do
+ not attempt ill-formed static_cast to pointers to non-object types.
+ * testsuite/20_util/any/misc/any_cast.cc: Test std::any_cast with
+ function types.
+
* testsuite/20_util/variant/run.cc: Catch exception by reference to
prevent -Wcatch-value warning.
template<typename _ValueType>
inline const _ValueType* any_cast(const any* __any) noexcept
{
- if (__any)
- return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
+ if constexpr (is_object_v<_ValueType>)
+ if (__any)
+ return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
return nullptr;
}
template<typename _ValueType>
inline _ValueType* any_cast(any* __any) noexcept
{
- if (__any)
- return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
+ if constexpr (is_object_v<_ValueType>)
+ if (__any)
+ return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
return nullptr;
}
// @}
#include <any>
#include <string>
+#include <utility>
#include <cstring>
#include <testsuite_hooks.h>
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&) {
+ }
+}
+
int main()
{
test01();
test03();
test04();
test05();
+ test06();
}