aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2024-06-18 16:59:52 +0100
committerJonathan Wakely <jwakely@redhat.com>2024-06-21 10:19:35 +0100
commit466ee78e3e975627440992dac67973ee314a0551 (patch)
tree778479b8c610c9ad2c1e11d651cfd4fabc601ab8
parentc3e237338eb7ffc90f3cc8d32a3971d17f6d0b31 (diff)
downloadgcc-466ee78e3e975627440992dac67973ee314a0551.zip
gcc-466ee78e3e975627440992dac67973ee314a0551.tar.gz
gcc-466ee78e3e975627440992dac67973ee314a0551.tar.bz2
libstdc++: Make std::any_cast<void> ill-formed (LWG 3305)
LWG 3305 was approved earlier this year in Tokyo. We need to give an error if using std::any_cast<void>, but std::any_cast<void()> is valid (but always returns null). libstdc++-v3/ChangeLog: * include/std/any (any_cast(any*), any_cast(const any*)): Add static assertion to reject void types, as per LWG 3305. * testsuite/20_util/any/misc/lwg3305.cc: New test.
-rw-r--r--libstdc++-v3/include/std/any8
-rw-r--r--libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc15
2 files changed, 23 insertions, 0 deletions
diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any
index 690ddc2..e4709b1c 100644
--- a/libstdc++-v3/include/std/any
+++ b/libstdc++-v3/include/std/any
@@ -554,6 +554,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _ValueType>
inline const _ValueType* any_cast(const any* __any) noexcept
{
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 3305. any_cast<void>
+ static_assert(!is_void_v<_ValueType>);
+
+ // As an optimization, don't bother instantiating __any_caster for
+ // function types, since std::any can only hold objects.
if constexpr (is_object_v<_ValueType>)
if (__any)
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
@@ -563,6 +569,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _ValueType>
inline _ValueType* any_cast(any* __any) noexcept
{
+ static_assert(!is_void_v<_ValueType>);
+
if constexpr (is_object_v<_ValueType>)
if (__any)
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
diff --git a/libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc b/libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc
new file mode 100644
index 0000000..49f5d74
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc
@@ -0,0 +1,15 @@
+// { dg-do compile { target c++17 } }
+
+// LWG 3305. any_cast<void>
+
+#include <any>
+
+void
+test_lwg3305()
+{
+ std::any a;
+ (void) std::any_cast<const void>(&a); // { dg-error "here" }
+ const std::any a2;
+ (void) std::any_cast<volatile void>(&a2); // { dg-error "here" }
+}
+// { dg-error "static assertion failed" "" { target *-*-* } 0 }