[multiple changes]
authorJonathan Wakely <redi@gcc.gnu.org>
Sat, 5 Jun 2010 17:43:29 +0000 (18:43 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Sat, 5 Jun 2010 17:43:29 +0000 (18:43 +0100)
2010-06-05  Magnus Fromreide  <magfr@lysator.liu.se>
            Jonathan Wakely  <jwakely.gcc@gmail.com>

* include/std/functional (function): Add nullptr_t support.
* include/bits/shared_ptr.h (shared_ptr): Likewise.
* include/bits/unique_ptr.h (unique_ptr): Likewise.

2010-06-05  Jonathan Wakely  <jwakely.gcc@gmail.com>

* include/bits/shared_ptr_base.h (__shared_ptr): Add nullptr_t support.
(__shared_count): Add nullptr_t support and make constructors explicit.
* testsuite/20_util/shared_ptr/cons/43820.cc: Adjust.
* testsuite/20_util/shared_ptr/cons/nullptr.cc: New.
* testsuite/20_util/unique_ptr/cons/nullptr.cc: New.
* testsuite/20_util/unique_ptr/assign/nullptr.cc: New.
* testsuite/20_util/unique_ptr/assign/assign_neg.cc: Adjust.
* testsuite/20_util/unique_ptr/modifiers/reset_neg.cc: Adjust.
* testsuite/20_util/weak_ptr/comparison/cmp_neg.cc: Adjust.

From-SVN: r160324

libstdc++-v3/include/bits/shared_ptr.h
libstdc++-v3/include/bits/shared_ptr_base.h
libstdc++-v3/include/bits/unique_ptr.h
libstdc++-v3/include/std/functional
libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820.cc
libstdc++-v3/testsuite/20_util/shared_ptr/cons/nullptr.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/unique_ptr/assign/assign_neg.cc
libstdc++-v3/testsuite/20_util/unique_ptr/assign/nullptr.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/unique_ptr/cons/nullptr.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/unique_ptr/modifiers/reset_neg.cc
libstdc++-v3/testsuite/20_util/weak_ptr/comparison/cmp_neg.cc

index 5e1960012c9ef7777dab1a80b713b9a8208f4977..f01630e5347c84a5a60624d0475d10aa061f0f76 100644 (file)
@@ -122,6 +122,23 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       template<typename _Tp1, typename _Deleter>
        shared_ptr(_Tp1* __p, _Deleter __d) : __shared_ptr<_Tp>(__p, __d) { }
 
+      /**
+       *  @brief  Construct a %shared_ptr that owns a null pointer
+       *          and the deleter @a __d.
+       *  @param  __p  A null pointer constant.
+       *  @param  __d  A deleter.
+       *  @post   use_count() == 1 && get() == __p
+       *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
+       *
+       *  Requirements: _Deleter's copy constructor and destructor must
+       *  not throw
+       *
+       *  The last owner will call __d(__p)
+       */
+      template<typename _Deleter>
+       shared_ptr(nullptr_t __p, _Deleter __d)
+        : __shared_ptr<_Tp>(__p, __d) { }
+
       /**
        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
        *          and the deleter @a __d.
@@ -141,6 +158,25 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
        : __shared_ptr<_Tp>(__p, __d, __a) { }
 
+      /**
+       *  @brief  Construct a %shared_ptr that owns a null pointer
+       *          and the deleter @a __d.
+       *  @param  __p  A null pointer constant.
+       *  @param  __d  A deleter.
+       *  @param  __a  An allocator.
+       *  @post   use_count() == 1 && get() == __p
+       *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
+       *
+       *  Requirements: _Deleter's copy constructor and destructor must
+       *  not throw _Alloc's copy constructor and destructor must not
+       *  throw.
+       *
+       *  The last owner will call __d(__p)
+       */
+      template<typename _Deleter, typename _Alloc>
+       shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
+       : __shared_ptr<_Tp>(__p, __d, __a) { }
+
       // Aliasing constructor
 
       /**
@@ -212,6 +248,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
        : __shared_ptr<_Tp>(std::move(__r)) { }
 
+      /**
+       *  @brief  Construct an empty %shared_ptr.
+       *  @param  __p  A null pointer constant.
+       *  @post   use_count() == 0 && get() == nullptr
+       */
+      shared_ptr(nullptr_t __p) : __shared_ptr<_Tp>(__p) { }
+
       template<typename _Tp1>
        shared_ptr&
        operator=(const shared_ptr<_Tp1>& __r) // never throws
index 0c366b8ef1a58f98834f9ec2cace9032a43a908a..cac1a70c1678613eae3155af8539b4bf1095cab6 100644 (file)
@@ -85,6 +85,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
     {
     public:
+      explicit
       _Sp_counted_ptr(_Ptr __p)
       : _M_ptr(__p) { }
 
@@ -107,6 +108,18 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       _Ptr             _M_ptr;  // copy constructor must not throw
     };
 
+  template<>
+    void
+    _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() { }
+
+  template<>
+    void
+    _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() { }
+
+  template<>
+    void
+    _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() { }
+
   // Support for custom deleter and/or allocator
   template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
     class _Sp_counted_deleter : public _Sp_counted_ptr<_Ptr, _Lp>
@@ -181,6 +194,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        _Base_type;
 
     public:
+      explicit
       _Sp_counted_ptr_inplace(_Alloc __a)
       : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
       , _M_storage()
@@ -237,6 +251,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       { }
 
       template<typename _Ptr>
+        explicit
        __shared_count(_Ptr __p) : _M_pi(0)
        {
          __try
@@ -316,6 +331,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 #if _GLIBCXX_DEPRECATED
       // Special case for auto_ptr<_Tp> to provide the strong guarantee.
       template<typename _Tp>
+        explicit
        __shared_count(std::auto_ptr<_Tp>&& __r)
        : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
        { __r.release(); }
@@ -323,6 +339,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
       // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
       template<typename _Tp, typename _Del>
+        explicit
        __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
        : _M_pi(_S_create_from_up(std::move(__r)))
        { __r.release(); }
@@ -567,6 +584,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
          __enable_shared_from_this_helper(_M_refcount, __p, __p);
        }
 
+      template<typename _Deleter>
+       __shared_ptr(nullptr_t __p, _Deleter __d)
+       : _M_ptr(0), _M_refcount(__p, __d)
+       { }
+
+      template<typename _Deleter, typename _Alloc>
+       __shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
+       : _M_ptr(0), _M_refcount(__p, __d, __a)
+       { }
+
       template<typename _Tp1>
        __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
        : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
@@ -631,6 +658,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
 #endif
 
+      /* TODO: use delegating constructor */
+      __shared_ptr(nullptr_t) : _M_ptr(0), _M_refcount() // never throws
+      { }
+
       template<typename _Tp1>
        __shared_ptr&
        operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
index 7b07ca477f4533d5cc8001d153ef7d2fb2b65b12..77d3f607dfdb9252b217fa11ea2d6151fa7e038e 100644 (file)
@@ -81,7 +81,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     class unique_ptr
     {
       typedef std::tuple<_Tp*, _Tp_Deleter>  __tuple_type;
-      typedef _Tp* unique_ptr::*             __unspecified_pointer_type;
 
       // use SFINAE to determine whether _Del::pointer exists
       class _Pointer
@@ -126,6 +125,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       { static_assert(!std::is_reference<deleter_type>::value, 
                      "rvalue deleter bound to reference"); }
 
+      unique_ptr(nullptr_t)
+      : _M_t(pointer(), deleter_type())
+      { }
+
       // Move constructors.
       unique_ptr(unique_ptr&& __u) 
       : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
@@ -157,7 +160,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
         }
 
       unique_ptr&
-      operator=(__unspecified_pointer_type) 
+      operator=(nullptr_t)
       {
        reset();
        return *this;
@@ -234,7 +237,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     class unique_ptr<_Tp[], _Tp_Deleter>
     {
       typedef std::tuple<_Tp*, _Tp_Deleter>  __tuple_type;
-      typedef _Tp* unique_ptr::*             __unspecified_pointer_type;
 
     public:
       typedef _Tp*               pointer;
@@ -264,6 +266,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       { static_assert(!std::is_reference<deleter_type>::value, 
                      "rvalue deleter bound to reference"); }
 
+      /* TODO: use delegating constructor */
+      unique_ptr(nullptr_t)
+      : _M_t(pointer(), deleter_type())
+      { }
+
       // Move constructors.
       unique_ptr(unique_ptr&& __u) 
       : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
@@ -295,7 +302,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
         }
 
       unique_ptr&
-      operator=(__unspecified_pointer_type)
+      operator=(nullptr_t)
       {
        reset();
        return *this;
@@ -338,7 +345,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       {
        using std::swap;
        swap(std::get<0>(_M_t), __p);
-       if (__p != pointer())
+       if (__p != nullptr)
+         get_deleter()(__p);
+      }
+
+      void
+      reset(nullptr_t)
+      {
+       pointer __p = get();
+       std::get<0>(_M_t) = pointer();
+       if (__p != nullptr)
          get_deleter()(__p);
       }
 
index b6cc6d5e0720134b97b83bbb2e2c1b828cfb9c67..752e78325a3fe71fe7572f5cc2837bed3ea40254 100644 (file)
@@ -1383,13 +1383,6 @@ namespace std
    */
   class bad_function_call : public std::exception { };
 
-  /**
-   *  The integral constant expression 0 can be converted into a
-   *  pointer to this type. It is used by the function template to
-   *  accept NULL pointers.
-   */
-  struct _M_clear_type;
-
   /**
    *  Trait identifying "location-invariant" types, meaning that the
    *  address of the object (or any of its members) will not escape.
@@ -1797,10 +1790,10 @@ namespace std
       function() : _Function_base() { }
       
       /**
-       *  @brief Default construct creates an empty function call wrapper.
+       *  @brief Creates an empty function call wrapper.
        *  @post @c !(bool)*this
        */
-      function(_M_clear_type*) : _Function_base() { }
+      function(nullptr_t) : _Function_base() { }
       
       /**
        *  @brief %Function copy constructor.
@@ -1893,7 +1886,7 @@ namespace std
        *  The target of @c *this is deallocated, leaving it empty.
        */
       function&
-      operator=(_M_clear_type*)
+      operator=(nullptr_t)
       {
         if (_M_manager)
          {
@@ -2139,13 +2132,13 @@ namespace std
    */
   template<typename _Res, typename... _Args>
     inline bool
-    operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
+    operator==(const function<_Res(_Args...)>& __f, nullptr_t)
     { return !static_cast<bool>(__f); }
 
   /// @overload
   template<typename _Res, typename... _Args>
     inline bool
-    operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
+    operator==(nullptr_t, const function<_Res(_Args...)>& __f)
     { return !static_cast<bool>(__f); }
 
   /**
@@ -2157,13 +2150,13 @@ namespace std
    */
   template<typename _Res, typename... _Args>
     inline bool
-    operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
+    operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
     { return static_cast<bool>(__f); }
 
   /// @overload
   template<typename _Res, typename... _Args>
     inline bool
-    operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
+    operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
     { return static_cast<bool>(__f); }
 
   // [20.7.15.2.7] specialized algorithms
index 6350079983842b89938addfa1f9b2f340760f4cc..c9c758442dbf7b4a9c7a1df9f25c5ed3f06c63a0 100644 (file)
@@ -32,10 +32,10 @@ void test01()
 {
   X* px = 0;
   std::shared_ptr<X> p1(px);   // { dg-error "here" }
-  // { dg-error "incomplete" "" { target *-*-* } 549 }
+  // { dg-error "incomplete" "" { target *-*-* } 566 }
 
   std::shared_ptr<X> p9(ap());  // { dg-error "here" }
-  // { dg-error "incomplete" "" { target *-*-* } 630 }
+  // { dg-error "incomplete" "" { target *-*-* } 657 }
 
 }
 
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/nullptr.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/nullptr.cc
new file mode 100644 (file)
index 0000000..82acaed
--- /dev/null
@@ -0,0 +1,93 @@
+// { dg-options "-std=gnu++0x" }
+
+// 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.11.2 Class template shared_ptr [util.smartptr.shared]
+
+#include <memory>
+#include <cstddef>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+// 20.9.11.2.1 shared_ptr constructors [util.smartptr.shared.const]
+
+// Construction from nullptr
+
+struct deleter
+{
+  int count;
+  deleter() : count(0) { }
+  void operator()(std::nullptr_t) { ++count; }
+  void operator()(int*) const { throw "wrong type passed to deleter"; }
+};
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  std::shared_ptr<int> p = nullptr;
+  VERIFY( p.get() == nullptr );
+  VERIFY( p.use_count() == 0 );
+
+}
+
+void
+test02()
+{
+  bool test __attribute__((unused)) = true;
+
+  deleter d;
+  std::shared_ptr<int> p(nullptr, std::ref(d));
+  VERIFY( p.get() == nullptr );
+  VERIFY( p.use_count() == 1 );
+
+  p = nullptr;
+  VERIFY( p.use_count() == 0 );
+  VERIFY( d.count == 1 );
+}
+
+
+void
+test03()
+{
+  bool test __attribute__((unused)) = true;
+
+  deleter d;
+  __gnu_test::tracker_allocator<int> a;
+  std::shared_ptr<int> p(nullptr, std::ref(d), a);
+  VERIFY( p.get() == nullptr );
+  VERIFY( p.use_count() == 1 );
+
+  p = nullptr;
+  VERIFY( p.use_count() == 0 );
+  VERIFY( d.count == 1 );
+
+  typedef __gnu_test::tracker_allocator_counter c;
+  VERIFY( c::get_destruct_count() == c::get_construct_count() );
+  VERIFY( c::get_deallocation_count() == c::get_allocation_count() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  return 0;
+}
index 4aa93b91d9135e3278064f54f06e91b5a4c8d8bb..40404f6bffd3c7d2fad32e8fdab5833eb0d28bde 100644 (file)
@@ -49,14 +49,15 @@ test03()
   std::unique_ptr<int[2]> p2 = p1;
 }
 
-// { dg-error "deleted function" "" { target *-*-* } 357 }
+// { dg-error "deleted function" "" { target *-*-* } 373 }
 // { dg-error "used here" "" { target *-*-* } 42 }
 // { dg-error "no matching" "" { target *-*-* } 48 }
-// { dg-warning "note" "" { target *-*-* } 130 }
-// { dg-warning "note" "" { target *-*-* } 134 }
-// { dg-warning "note" "" { target *-*-* } 123 }
-// { dg-warning "note" "" { target *-*-* } 118 }
-// { dg-warning "note" "" { target *-*-* } 113 }
-// { dg-warning "note" "" { target *-*-* } 107 }
-// { dg-error "deleted function" "" { target *-*-* } 222 }
+// { dg-warning "note" "" { target *-*-* } 137 }
+// { dg-warning "note" "" { target *-*-* } 133 }
+// { dg-warning "note" "" { target *-*-* } 128 }
+// { dg-warning "note" "" { target *-*-* } 122 }
+// { dg-warning "note" "" { target *-*-* } 117 }
+// { dg-warning "note" "" { target *-*-* } 112 }
+// { dg-warning "note" "" { target *-*-* } 106 }
+// { dg-error "deleted function" "" { target *-*-* } 225 }
 // { dg-error "used here" "" { target *-*-* } 49 }
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/assign/nullptr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/nullptr.cc
new file mode 100644 (file)
index 0000000..6f067ee
--- /dev/null
@@ -0,0 +1,54 @@
+// { dg-options "-std=gnu++0x" }
+
+// 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 Class template unique_ptr [unique.ptr]
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+struct A { };
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  std::unique_ptr<A> p(new A);
+  p = nullptr;
+
+  VERIFY( p.get() == nullptr );
+}
+
+void
+test02()
+{
+  bool test __attribute__((unused)) = true;
+
+  std::unique_ptr<A[]> p(new A[2]);
+  p = nullptr;
+
+  VERIFY( p.get() == nullptr );
+}
+
+int main()
+{
+  test01();
+  test02();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/cons/nullptr.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/nullptr.cc
new file mode 100644 (file)
index 0000000..1f515ea
--- /dev/null
@@ -0,0 +1,52 @@
+// { dg-options "-std=gnu++0x" }
+
+// 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 Class template unique_ptr [unique.ptr]
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+struct A { };
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  std::unique_ptr<A> p = nullptr;
+
+  VERIFY( p.get() == nullptr );
+}
+
+void
+test02()
+{
+  bool test __attribute__((unused)) = true;
+
+  std::unique_ptr<A[]> p = nullptr;
+
+  VERIFY( p.get() == nullptr );
+}
+
+int main()
+{
+  test01();
+  test02();
+  return 0;
+}
index 3b389b9da684dc8a44fcfab8a3b58f89eae2fd6b..27dd87915369fe21962c2171e95b665bda7b5eb0 100644 (file)
@@ -36,4 +36,4 @@ void test01()
 }
 
 // { dg-error "used here" "" { target *-*-* } 35 } 
-// { dg-error "deleted function" "" { target *-*-* } 347 }
+// { dg-error "deleted function" "" { target *-*-* } 363 }
index cbe5f9fdbe89d8f1585484c643372764f4eaed59..b3a45b11018ef8e96cb9d07c3265967d8d2de81e 100644 (file)
@@ -41,9 +41,9 @@ main()
   return 0;
 }
 
-// { dg-warning "note" "" { target *-*-* } 281 }
-// { dg-warning "note" "" { target *-*-* } 407 }
-// { dg-warning "note" "" { target *-*-* } 831 }
+// { dg-warning "note" "" { target *-*-* } 324 }
+// { dg-warning "note" "" { target *-*-* } 423 }
+// { dg-warning "note" "" { target *-*-* } 862 }
 // { dg-warning "note" "" { target *-*-* } 511 }
 // { dg-warning "note" "" { target *-*-* } 1005 }
 // { dg-warning "note" "" { target *-*-* } 340 }