public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix constraints for std::variant default constructor
Date: Fri, 15 Sep 2023 10:27:38 +0100	[thread overview]
Message-ID: <20230915093220.2192265-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk.

Thanks to Nina Ranns for noticing this bug.

Removing the base class isn't an ABI change. It's an empty base class
with a type that's unique to the std::variant that derives from it, so
doesn't change whether two std::variant subobjects can be overlapping.

-- >8 --

The standard says the default ctor should be constrained, not deleted.
Our use of a defaulted default ctor and _Enable_default_constructor base
class results in it being deleted.

libstdc++-v3/ChangeLog:

	* include/std/variant (variant): Remove derivation from
	_Enable_default_constructor base class.
	(variant::variant()): Constrain.
	* testsuite/20_util/variant/default_ctor.cc: New test.
---
 libstdc++-v3/include/std/variant              | 24 +++++++++----------
 .../testsuite/20_util/variant/default_ctor.cc | 11 +++++++++
 2 files changed, 23 insertions(+), 12 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/variant/default_ctor.cc

diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 36eec8d829b..c0e41740dcf 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -1332,9 +1332,6 @@ namespace __variant
   template<typename... _Types>
     class variant
     : private __detail::__variant::_Variant_base<_Types...>,
-      private _Enable_default_constructor<
-	__detail::__variant::_Traits<_Types...>::_S_default_ctor,
-	  variant<_Types...>>,
       private _Enable_copy_move<
 	__detail::__variant::_Traits<_Types...>::_S_copy_ctor,
 	__detail::__variant::_Traits<_Types...>::_S_copy_assign,
@@ -1355,10 +1352,6 @@ namespace __variant
 		    "variant must have no void alternative");
 
       using _Base = __detail::__variant::_Variant_base<_Types...>;
-      using _Default_ctor_enabler =
-	_Enable_default_constructor<
-	  __detail::__variant::_Traits<_Types...>::_S_default_ctor,
-	    variant<_Types...>>;
 
       template<typename _Tp>
 	static constexpr bool __not_self
@@ -1396,7 +1389,16 @@ namespace __variant
 	  = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
 
     public:
-      variant() = default;
+#if __cpp_concepts
+      variant() requires is_default_constructible_v<__to_type<0>> = default;
+#else
+      template<typename _Tp0 = __to_type<0>,
+	       typename = enable_if_t<is_default_constructible_v<_Tp0>>>
+	constexpr
+	variant() noexcept(is_nothrow_default_constructible_v<__to_type<0>>)
+	{ }
+#endif
+
       variant(const variant& __rhs) = default;
       variant(variant&&) = default;
       variant& operator=(const variant&) = default;
@@ -1441,8 +1443,7 @@ namespace __variant
 	       typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
 	constexpr explicit
 	variant(in_place_index_t<_Np>, _Args&&... __args)
-	: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
-	_Default_ctor_enabler(_Enable_default_constructor_tag{})
+	: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...)
 	{ }
 
       template<size_t _Np, typename _Up, typename... _Args,
@@ -1453,8 +1454,7 @@ namespace __variant
 	constexpr explicit
 	variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
 		_Args&&... __args)
-	: _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
-	_Default_ctor_enabler(_Enable_default_constructor_tag{})
+	: _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...)
 	{ }
 
       template<typename _Tp>
diff --git a/libstdc++-v3/testsuite/20_util/variant/default_ctor.cc b/libstdc++-v3/testsuite/20_util/variant/default_ctor.cc
new file mode 100644
index 00000000000..d0d9235a59c
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/variant/default_ctor.cc
@@ -0,0 +1,11 @@
+// { dg-do compile { target c++17 } }
+
+#include <variant>
+
+struct A{ A(int); };
+struct B{ B(){}; };
+
+void f(std::variant<A>);
+int f(B);
+
+int unambiguous = f({}); // { dg-bogus "ambiguous" }
-- 
2.41.0


                 reply	other threads:[~2023-09-15  9:32 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=20230915093220.2192265-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@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).