public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Make std::any_cast<void> ill-formed (LWG 3305)
@ 2024-06-20 15:36 Jonathan Wakely
  2024-06-21  9:22 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2024-06-20 15:36 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux.

-- >8 --

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.
---
 libstdc++-v3/include/std/any                      |  8 ++++++++
 .../testsuite/20_util/any/misc/lwg3305.cc         | 15 +++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc

diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any
index 690ddc2aa57..e4709b1ce04 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 00000000000..49f5d747ab3
--- /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 }
-- 
2.45.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] libstdc++: Make std::any_cast<void> ill-formed (LWG 3305)
  2024-06-20 15:36 [PATCH] libstdc++: Make std::any_cast<void> ill-formed (LWG 3305) Jonathan Wakely
@ 2024-06-21  9:22 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-06-21  9:22 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Pushed to trunk now. I might backport this later too.


On Thu, 20 Jun 2024 at 16:39, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> Tested x86_64-linux.
>
> -- >8 --
>
> 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.
> ---
>  libstdc++-v3/include/std/any                      |  8 ++++++++
>  .../testsuite/20_util/any/misc/lwg3305.cc         | 15 +++++++++++++++
>  2 files changed, 23 insertions(+)
>  create mode 100644 libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc
>
> diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any
> index 690ddc2aa57..e4709b1ce04 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 00000000000..49f5d747ab3
> --- /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 }
> --
> 2.45.2
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-21  9:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-20 15:36 [PATCH] libstdc++: Make std::any_cast<void> ill-formed (LWG 3305) Jonathan Wakely
2024-06-21  9:22 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).