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.
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
/**
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
class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
{
public:
+ explicit
_Sp_counted_ptr(_Ptr __p)
: _M_ptr(__p) { }
_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>
_Base_type;
public:
+ explicit
_Sp_counted_ptr_inplace(_Alloc __a)
: _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
, _M_storage()
{ }
template<typename _Ptr>
+ explicit
__shared_count(_Ptr __p) : _M_pi(0)
{
__try
#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(); }
// 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(); }
__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
}
#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
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
{ 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())) { }
}
unique_ptr&
- operator=(__unspecified_pointer_type)
+ operator=(nullptr_t)
{
reset();
return *this;
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;
{ 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())) { }
}
unique_ptr&
- operator=(__unspecified_pointer_type)
+ operator=(nullptr_t)
{
reset();
return *this;
{
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);
}
*/
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.
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.
* The target of @c *this is deallocated, leaving it empty.
*/
function&
- operator=(_M_clear_type*)
+ operator=(nullptr_t)
{
if (_M_manager)
{
*/
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); }
/**
*/
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
{
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 }
}
--- /dev/null
+// { 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;
+}
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 }
--- /dev/null
+// { 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;
+}
--- /dev/null
+// { 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;
+}
}
// { dg-error "used here" "" { target *-*-* } 35 }
-// { dg-error "deleted function" "" { target *-*-* } 347 }
+// { dg-error "deleted function" "" { target *-*-* } 363 }
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 }