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

On Thu, 20 Jun 2024 at 16:39, Jonathan Wakely <jwak...@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
>

Reply via email to