diff mbox

PR77528 add default constructors for container adaptors

Message ID 20170112172938.GX13348@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Jan. 12, 2017, 5:29 p.m. UTC
On 11/01/17 13:25 +0000, Jonathan Wakely wrote:
>On 11/01/17 08:04 -0500, Tim Song wrote:
>>On Wed, Jan 11, 2017 at 7:21 AM, Jonathan Wakely <jwakely@redhat.com> wrote:
>>>This patch uses the _Enable_default_constructor mixin to properly
>>>delete the default constructors. It's a bit cumbersome, because we
>>>have to add an initializer for the base class to every
>>>ctor-initializer-list, but I think I prefer this to making the default
>>>constructor a constrained template.
>>>
>>
>>I'm happy with either approach - my primary concern is making sure
>>that is_constructible and friends work and don't lie, in a world where
>>increasing numbers of library components depend on it. Though I'm a
>
>Yes, it's important that we give the right answer.
>
>>bit curious as to why you found this approach more preferable.
>
>I dislike making functions into templates when they aren't "supposed"
>to be. But I'm in two minds for this case. It's certainly a smaller,
>more self-contained change to just add a default constructor template
>and not mess about with a new base class and DMIs and all those
>mem-initializers.
>
>>Re the new DMI, my brain compiler says that _Sequence c = _Sequence();
>>breaks anything with an explicit copy/move constructor pre-C++17, but
>>I also don't think we care about those, right?
>
>I dislike them, but maybe the fact they won't work here is a strong
>enough reason to get over my dislike of the original approach and just
>do it that way instead.

I decided to go with the original solution shown in the PR.

Tested powerpc64le-linux, committed to trunk.
diff mbox

Patch

commit b4614f1ef1a9c98650f2454332bb1407cddff13c
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jan 12 15:37:27 2017 +0000

    PR77528 partially revert r244278 and define default constructors
    
    	PR libstdc++/77528
    	* include/bits/stl_queue.h (queue, priority_queue): Remove default
    	member-initializers and define default constructors as templates with
    	constraints.
    	* include/bits/stl_stack.h (stack): Likewise.
    	* testsuite/23_containers/priority_queue/requirements/constructible.cc:
    	New.
    	* testsuite/23_containers/priority_queue/requirements/
    	explicit_instantiation/1.cc: Test more instantiations.
    	* testsuite/23_containers/priority_queue/requirements/
    	explicit_instantiation/1_c++98.cc: Likewise.
    	* testsuite/23_containers/queue/requirements/constructible.cc: New.
    	* testsuite/23_containers/stack/requirements/constructible.cc: New.

diff --git a/libstdc++-v3/include/bits/stl_queue.h b/libstdc++-v3/include/bits/stl_queue.h
index 6417b30..3a52367 100644
--- a/libstdc++-v3/include/bits/stl_queue.h
+++ b/libstdc++-v3/include/bits/stl_queue.h
@@ -131,12 +131,8 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  of private: to allow derivation.  But none of the other
        *  containers allow for derivation.  Odd.)
        */
-      /// @c c is the underlying container.
-#if __cplusplus >= 201103L
-      _Sequence c{};
-#else
+       ///  @c c is the underlying container.
       _Sequence c;
-#endif
 
     public:
       /**
@@ -147,7 +143,10 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       queue(const _Sequence& __c = _Sequence())
       : c(__c) { }
 #else
-      queue() = default;
+      template<typename _Seq = _Sequence, typename _Requires = typename
+	       enable_if<is_default_constructible<_Seq>::value>::type>
+	queue()
+	: c() { }
 
       explicit
       queue(const _Sequence& __c)
@@ -446,13 +445,8 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     protected:
       //  See queue::c for notes on these names.
-#if __cplusplus >= 201103L
-      _Sequence c{};
-      _Compare   comp{};
-#else
-      _Sequence c;
+      _Sequence  c;
       _Compare   comp;
-#endif
 
     public:
       /**
@@ -465,17 +459,19 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       : c(__s), comp(__x)
       { std::make_heap(c.begin(), c.end(), comp); }
 #else
-      priority_queue() = default;
+      template<typename _Seq = _Sequence, typename _Requires = typename
+	       enable_if<__and_<is_default_constructible<_Compare>,
+                                is_default_constructible<_Seq>>::value>::type>
+	priority_queue()
+	: c(), comp() { }
 
       explicit
-      priority_queue(const _Compare& __x,
-		     const _Sequence& __s)
+      priority_queue(const _Compare& __x, const _Sequence& __s)
       : c(__s), comp(__x)
       { std::make_heap(c.begin(), c.end(), comp); }
 
       explicit
-      priority_queue(const _Compare& __x,
-		     _Sequence&& __s = _Sequence())
+      priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence())
       : c(std::move(__s)), comp(__x)
       { std::make_heap(c.begin(), c.end(), comp); }
 
diff --git a/libstdc++-v3/include/bits/stl_stack.h b/libstdc++-v3/include/bits/stl_stack.h
index a0f9ee5..094ce65 100644
--- a/libstdc++-v3/include/bits/stl_stack.h
+++ b/libstdc++-v3/include/bits/stl_stack.h
@@ -129,11 +129,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     protected:
       //  See queue::c for notes on this name.
-#if __cplusplus >= 201103L
-      _Sequence c{};
-#else
       _Sequence c;
-#endif
 
     public:
       // XXX removed old def ctor, added def arg to this one to match 14882
@@ -145,7 +141,10 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       stack(const _Sequence& __c = _Sequence())
       : c(__c) { }
 #else
-      stack() = default;
+      template<typename _Seq = _Sequence, typename _Requires = typename
+	       enable_if<is_default_constructible<_Seq>::value>::type>
+	stack()
+	: c() { }
 
       explicit
       stack(const _Sequence& __c)
diff --git a/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/constructible.cc b/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/constructible.cc
new file mode 100644
index 0000000..fa412f3
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/constructible.cc
@@ -0,0 +1,49 @@ 
+// { dg-do compile }
+
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+
+// This file tests explicit instantiation of library containers.
+
+#include <queue>
+
+using std::priority_queue;
+using std::vector;
+
+template<typename A>
+constexpr bool default_constructible()
+{ return std::is_default_constructible<A>::value; }
+
+static_assert(default_constructible<priority_queue<int>>(),
+	      "priority_queue<int>");
+
+struct NonDefaultConstructible : vector<int> {
+  NonDefaultConstructible(int) { }
+};
+struct Cmp : std::less<int> {
+  Cmp(int) { }
+};
+static_assert(
+    !default_constructible<priority_queue<int, NonDefaultConstructible>>(),
+    "priority_queue<int, NonDefaultConstructible>");
+static_assert(
+    !default_constructible<priority_queue<int, NonDefaultConstructible, Cmp>>(),
+    "priority_queue<int, NonDefaultConstructible, Cmp>");
+static_assert(
+    !default_constructible<priority_queue<int, vector<int>, Cmp>>(),
+    "priority_queue<int, vector<int>, Cmp>");
diff --git a/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1.cc
index 77cade0..6386d1d 100644
--- a/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1.cc
+++ b/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1.cc
@@ -31,3 +31,5 @@  struct Cmp : std::less<int> {
   Cmp(int) { }
 };
 template class std::priority_queue<int, NonDefaultConstructible>;
+template class std::priority_queue<int, NonDefaultConstructible, Cmp>;
+template class std::priority_queue<int, std::vector<int>, Cmp>;
diff --git a/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1_c++98.cc b/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1_c++98.cc
index 0b5b287..c9530cb 100644
--- a/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1_c++98.cc
+++ b/libstdc++-v3/testsuite/23_containers/priority_queue/requirements/explicit_instantiation/1_c++98.cc
@@ -30,4 +30,6 @@  struct NonDefaultConstructible : std::vector<int> {
 struct Cmp : std::less<int> {
   Cmp(int) { }
 };
+template class std::priority_queue<int, NonDefaultConstructible>;
 template class std::priority_queue<int, NonDefaultConstructible, Cmp>;
+template class std::priority_queue<int, std::vector<int>, Cmp>;
diff --git a/libstdc++-v3/testsuite/23_containers/queue/requirements/constructible.cc b/libstdc++-v3/testsuite/23_containers/queue/requirements/constructible.cc
new file mode 100644
index 0000000..99a8b84
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/queue/requirements/constructible.cc
@@ -0,0 +1,37 @@ 
+// { dg-do compile }
+
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+
+// This file tests explicit instantiation of library containers.
+
+#include <queue>
+
+using std::queue;
+
+template<typename A>
+constexpr bool default_constructible()
+{ return std::is_default_constructible<A>::value; }
+
+static_assert(default_constructible<queue<int>>(), "queue<int>");
+
+struct NonDefaultConstructible : std::deque<int> {
+  NonDefaultConstructible(int) { }
+};
+static_assert(!default_constructible<queue<int, NonDefaultConstructible>>(),
+	      "queue<int, NonDefaultConstructible>");
diff --git a/libstdc++-v3/testsuite/23_containers/stack/requirements/constructible.cc b/libstdc++-v3/testsuite/23_containers/stack/requirements/constructible.cc
new file mode 100644
index 0000000..0d6e174
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/stack/requirements/constructible.cc
@@ -0,0 +1,37 @@ 
+// { dg-do compile }
+
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+
+// This file tests explicit instantiation of library containers.
+
+#include <stack>
+
+using std::stack;
+
+template<typename A>
+constexpr bool default_constructible()
+{ return std::is_default_constructible<A>::value; }
+
+static_assert(default_constructible<stack<int>>(), "stack<int>");
+
+struct NonDefaultConstructible : std::deque<int> {
+  NonDefaultConstructible(int) { }
+};
+static_assert(!default_constructible<stack<int, NonDefaultConstructible>>(),
+	      "stack<int, NonDefaultConstructible>");