Patchwork [v3] Add constexpr specifiers to std::shared_ptr, std::unique_ptr, etc

login
register
mail settings
Submitter Jonathan Wakely
Date Nov. 8, 2010, 11:42 p.m.
Message ID <AANLkTimWAMa20P_9ZGJ01N7j-CszQqeP79WKtV-FS9W5@mail.gmail.com>
Download mbox | patch
Permalink /patch/70480/
State New
Headers show

Comments

Jonathan Wakely - Nov. 8, 2010, 11:42 p.m.
Here's the fix to allow unique_ptr to use deleters of pointer type.

ptr_deleter.cc tests the functionality, ptr_deleter_neg.cc checks that
it's ill-formed to construct such a unique_ptr without supplying a
deleter. That has XFAILs until we can use a static_assert in the
constexpr constructors, which depends on c++/46382

I've also changed the constexpr constructors to value-initialize the
tuple member, because only tuple's default constructor is constexpr.

2010-11-08  Jonathan Wakely  <jwakely.gcc@gmail.com>

        * include/bits/unique_ptr.h: Move misplaced static_assert and use
        tuple's constexpr constructor in constexpr constructors.
        * testsuite/20_util/unique_ptr/cons/ptr_deleter.cc: New.
        * testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc: New.
H.J. Lu - Nov. 9, 2010, 3:28 a.m.
On Mon, Nov 8, 2010 at 3:42 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> Here's the fix to allow unique_ptr to use deleters of pointer type.
>
> ptr_deleter.cc tests the functionality, ptr_deleter_neg.cc checks that
> it's ill-formed to construct such a unique_ptr without supplying a
> deleter. That has XFAILs until we can use a static_assert in the
> constexpr constructors, which depends on c++/46382
>
> I've also changed the constexpr constructors to value-initialize the
> tuple member, because only tuple's default constructor is constexpr.
>
> 2010-11-08  Jonathan Wakely  <jwakely.gcc@gmail.com>
>
>        * include/bits/unique_ptr.h: Move misplaced static_assert and use
>        tuple's constexpr constructor in constexpr constructors.
>        * testsuite/20_util/unique_ptr/cons/ptr_deleter.cc: New.
>        * testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc: New.
>

This may have caused:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46385
Jonathan Wakely - Nov. 9, 2010, 10:06 a.m.
On 9 November 2010 03:28, H.J. Lu  wrote:
>
> This may have caused:
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46385

Apologies - I had another change in my tree which
I haven't committed yet and thought the weak_ptr test failure was
related to that.  Thanks Paolo for fixing it.
Benjamin Kosnik - Nov. 9, 2010, 4:22 p.m.
> I've also changed the constexpr constructors to value-initialize the
> tuple member, because only tuple's default constructor is constexpr.

Thanks. I was concerned when the first change was incorrect by
inspection, and tests were removed.... this looks to be actually
functional now. 

That said, I don't really see a use for a default-constructed and
constexpr unique_ptr.... thoughts? I'm curious, and pre-coffee.

-benjamin
Paolo Carlini - Nov. 9, 2010, 4:26 p.m.
On 11/09/2010 05:22 PM, Benjamin Kosnik wrote:
>> I've also changed the constexpr constructors to value-initialize the
>> tuple member, because only tuple's default constructor is constexpr.
>>     
> Thanks. I was concerned when the first change was incorrect by
> inspection, and tests were removed....
>   
To be clear: those tests were obviously incorrect, because unique_ptr
and shared_ptr are not literal types.

Paolo.

Patch

Index: include/bits/unique_ptr.h
===================================================================
--- include/bits/unique_ptr.h	(revision 166456)
+++ include/bits/unique_ptr.h	(working copy)
@@ -106,9 +106,6 @@  _GLIBCXX_BEGIN_NAMESPACE(std)
       typedef _Tp                       element_type;
       typedef _Dp                       deleter_type;
 
-      static_assert(!std::is_pointer<deleter_type>::value,
-		    "constructed with null function pointer deleter");
-
       // Constructors.
       constexpr unique_ptr()
       : _M_t()
@@ -132,7 +129,7 @@  _GLIBCXX_BEGIN_NAMESPACE(std)
 		      "rvalue deleter bound to reference"); }
 
       constexpr unique_ptr(nullptr_t)
-      : _M_t(pointer(), deleter_type())
+      : _M_t()
       { }
 
       // Move constructors.
@@ -269,18 +266,16 @@  _GLIBCXX_BEGIN_NAMESPACE(std)
       typedef _Tp		 	element_type;
       typedef _Dp                       deleter_type;
 
-      static_assert(!std::is_pointer<deleter_type>::value,
-		    "constructed with null function pointer deleter");
-
       // Constructors.
       constexpr unique_ptr()
-      : _M_t(pointer(), deleter_type())
+      : _M_t()
       { }
 
       explicit
       unique_ptr(pointer __p)
       : _M_t(__p, deleter_type())
-      { }
+      { static_assert(!std::is_pointer<deleter_type>::value,
+		     "constructed with null function pointer deleter"); }
 
       unique_ptr(pointer __p,
 	  typename std::conditional<std::is_reference<deleter_type>::value,
@@ -295,7 +290,7 @@  _GLIBCXX_BEGIN_NAMESPACE(std)
 
       /* TODO: use delegating constructor */
       constexpr unique_ptr(nullptr_t)
-      : _M_t(pointer(), deleter_type())
+      : _M_t()
       { }
 
       // Move constructors.
Index: testsuite/20_util/unique_ptr/cons/ptr_deleter.cc
===================================================================
--- testsuite/20_util/unique_ptr/cons/ptr_deleter.cc	(revision 0)
+++ testsuite/20_util/unique_ptr/cons/ptr_deleter.cc	(revision 0)
@@ -0,0 +1,68 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do run }
+
+// Copyright (C) 2010 Free Software Foundation
+//
+// 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/>.
+
+// 20.9.10 Template class unique_ptr [unique.ptr]
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+static int count;
+
+void del(int* p) { ++count; delete p; }
+void vdel(int* p) { ++count; delete[] p; }
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+  count = 0;
+  {
+    std::unique_ptr<int, void(*)(int*)> p(nullptr, del);
+  }
+  VERIFY( count == 0 );
+  {
+    std::unique_ptr<int, void(*)(int*)> p(new int, del);
+  }
+  VERIFY( count == 1 );
+}
+
+void
+test02()
+{
+  bool test __attribute__((unused)) = true;
+  count = 0;
+  {
+    std::unique_ptr<int[], void(*)(int*)> p(nullptr, vdel);
+  }
+  VERIFY( count == 0 );
+  {
+    std::unique_ptr<int[], void(*)(int*)> p(new int[1], vdel);
+  }
+  VERIFY( count == 1 );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  return 0;
+}
+
Index: testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc
===================================================================
--- testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc	(revision 0)
+++ testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc	(revision 0)
@@ -0,0 +1,57 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+
+// Copyright (C) 2010 Free Software Foundation
+//
+// 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/>.
+
+// 20.6.11 Template class unique_ptr [unique.ptr]
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+using std::unique_ptr;
+
+// { dg-excess-errors "static assertion failed" }
+
+void
+test01()
+{
+  unique_ptr<int, void(*)(int*)> p1; // { dg-error "here" "" { xfail *-*-* } }
+
+  unique_ptr<int, void(*)(int*)> p2(nullptr); // { dg-error "here" "" { xfail *-*-* } }
+
+  unique_ptr<int, void(*)(int*)> p3(new int); // { dg-error "here" }
+}
+
+void
+test02()
+{
+  unique_ptr<int[], void(*)(int*)> p1; // { dg-error "here" "" { xfail *-*-* } }
+
+  unique_ptr<int[], void(*)(int*)> p2(nullptr); // { dg-error "here" "" { xfail *-*-* } }
+
+  unique_ptr<int[], void(*)(int*)> p3(new int[1]); // { dg-error "here" }
+}
+
+
+int
+main()
+{
+  test01();
+  test02();
+  return 0;
+}