public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-4634] libstdc++: Constrain std::make_any [PR102894]
Date: Fri, 22 Oct 2021 22:10:07 +0000 (GMT)	[thread overview]
Message-ID: <20211022221007.5D8EC3858416@sourceware.org> (raw)

https://gcc.gnu.org/g:0c1f737a485f05c591c94b50acfb416c45a4c916

commit r12-4634-g0c1f737a485f05c591c94b50acfb416c45a4c916
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Oct 22 22:55:00 2021 +0100

    libstdc++: Constrain std::make_any [PR102894]
    
    std::make_any should be constrained so it can only be called if the
    construction of the return value would be valid.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/102894
            * include/std/any (make_any): Add SFINAE constraint.
            * testsuite/20_util/any/102894.cc: New test.

Diff:
---
 libstdc++-v3/include/std/any                 | 13 +++++++++----
 libstdc++-v3/testsuite/20_util/any/102894.cc | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any
index 9c102a58b26..f75dddf6d92 100644
--- a/libstdc++-v3/include/std/any
+++ b/libstdc++-v3/include/std/any
@@ -428,16 +428,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   /// Exchange the states of two @c any objects.
   inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
 
-  /// Create an any holding a @c _Tp constructed from @c __args.
+  /// Create an `any` holding a `_Tp` constructed from `__args...`.
   template <typename _Tp, typename... _Args>
-    any make_any(_Args&&... __args)
+    inline
+    enable_if_t<is_constructible_v<any, in_place_type_t<_Tp>, _Args...>, any>
+    make_any(_Args&&... __args)
     {
       return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
     }
 
-  /// Create an any holding a @c _Tp constructed from @c __il and @c __args.
+  /// Create an `any` holding a `_Tp` constructed from `__il` and `__args...`.
   template <typename _Tp, typename _Up, typename... _Args>
-    any make_any(initializer_list<_Up> __il, _Args&&... __args)
+    inline
+    enable_if_t<is_constructible_v<any, in_place_type_t<_Tp>,
+				   initializer_list<_Up>&, _Args...>, any>
+    make_any(initializer_list<_Up> __il, _Args&&... __args)
     {
       return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
     }
diff --git a/libstdc++-v3/testsuite/20_util/any/102894.cc b/libstdc++-v3/testsuite/20_util/any/102894.cc
new file mode 100644
index 00000000000..66ea9a03fea
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/any/102894.cc
@@ -0,0 +1,20 @@
+// { dg-do compile { target c++17 } }
+#include <any>
+
+template<typename T, typename = void>
+struct can_make_any
+: std::false_type
+{ };
+
+template<typename T>
+struct can_make_any<T, std::void_t<decltype(std::make_any<T>())>>
+: std::true_type
+{ };
+
+struct move_only
+{
+  move_only() = default;
+  move_only(move_only&&) = default;
+};
+
+static_assert( ! can_make_any<move_only>::value ); // PR libstdc++/102894


                 reply	other threads:[~2021-10-22 22:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211022221007.5D8EC3858416@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).