From: Jonathan Wakely Date: Fri, 9 Jun 2017 13:20:37 +0000 (+0100) Subject: Support move-only deleters in std::shared_ptr (LWG 2802) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=53d01fd9e9568250e4304b5e0a5227e90f07cc40;p=gcc.git Support move-only deleters in std::shared_ptr (LWG 2802) * doc/xml/manual/intro.xml: Document LWG 2802, 2873 and 2942 changes. * include/bits/shared_ptr.h (shared_ptr): Use rvalues for deleters (LWG 2802). * include/bits/shared_ptr_base.h (_Sp_ebo_helper, _Sp_counted_deleter (_Sp_counted_deleter::_Impl, __shared_count, __shared_ptr): Likewise. * testsuite/20_util/shared_ptr/cons/lwg2802.cc: New. From-SVN: r249061 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 6909a1fe84a..0408210c0c5 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,12 @@ 2017-06-09 Jonathan Wakely + * doc/xml/manual/intro.xml: Document LWG 2802, 2873 and 2942 changes. + * include/bits/shared_ptr.h (shared_ptr): Use rvalues for deleters + (LWG 2802). + * include/bits/shared_ptr_base.h (_Sp_ebo_helper, _Sp_counted_deleter + (_Sp_counted_deleter::_Impl, __shared_count, __shared_ptr): Likewise. + * testsuite/20_util/shared_ptr/cons/lwg2802.cc: New. + * include/bits/forward_list.h (forward_list): Add deduction guide. * include/bits/stl_deque.h (deque): Likewise. * include/bits/stl_list.h (list): Likewise. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index b750f0a6785..4ec74949bfb 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -1119,6 +1119,29 @@ requirements of the license of GCC. arguments and store them directly as the target object. + 2802: + Add noexcept to several shared_ptr related + functions + + + Add noexcept. + + + 2873: + shared_ptr constructor requirements for a deleter + + + Use rvalues for deleters. + + + 2942: + LWG 2873's resolution missed + weak_ptr::owner_before + + + Add noexcept. + + diff --git a/libstdc++-v3/include/bits/shared_ptr.h b/libstdc++-v3/include/bits/shared_ptr.h index 2ddb221e2fe..264e35ce2d0 100644 --- a/libstdc++-v3/include/bits/shared_ptr.h +++ b/libstdc++-v3/include/bits/shared_ptr.h @@ -144,7 +144,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> shared_ptr(_Yp* __p, _Deleter __d) - : __shared_ptr<_Tp>(__p, __d) { } + : __shared_ptr<_Tp>(__p, std::move(__d)) { } /** * @brief Construct a %shared_ptr that owns a null pointer @@ -161,7 +161,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template shared_ptr(nullptr_t __p, _Deleter __d) - : __shared_ptr<_Tp>(__p, __d) { } + : __shared_ptr<_Tp>(__p, std::move(__d)) { } /** * @brief Construct a %shared_ptr that owns the pointer @a __p @@ -181,7 +181,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) - : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { } + : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } /** * @brief Construct a %shared_ptr that owns a null pointer @@ -200,7 +200,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) - : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { } + : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } // Aliasing constructor diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index f0916d05a06..a07058c59c4 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -418,6 +418,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp { explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { } + explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { } static _Tp& _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); } @@ -428,6 +429,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct _Sp_ebo_helper<_Nm, _Tp, false> { explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { } + explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { } static _Tp& _S_get(_Sp_ebo_helper& __eboh) @@ -448,7 +450,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION public: _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept - : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a) + : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a) { } _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); } @@ -462,11 +464,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // __d(__p) must not throw. _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept - : _M_impl(__p, __d, _Alloc()) { } + : _M_impl(__p, std::move(__d), _Alloc()) { } // __d(__p) must not throw. _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept - : _M_impl(__p, __d, __a) { } + : _M_impl(__p, std::move(__d), __a) { } ~_Sp_counted_deleter() noexcept { } @@ -1111,7 +1113,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> __shared_ptr(_Yp* __p, _Deleter __d) - : _M_ptr(__p), _M_refcount(__p, __d) + : _M_ptr(__p), _M_refcount(__p, std::move(__d)) { static_assert(__is_invocable<_Deleter&, _Yp*&>::value, "deleter expression d(p) is well-formed"); @@ -1121,7 +1123,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) - : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a)) + : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a)) { static_assert(__is_invocable<_Deleter&, _Yp*&>::value, "deleter expression d(p) is well-formed"); @@ -1130,12 +1132,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template __shared_ptr(nullptr_t __p, _Deleter __d) - : _M_ptr(0), _M_refcount(__p, __d) + : _M_ptr(0), _M_refcount(__p, std::move(__d)) { } template __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) - : _M_ptr(0), _M_refcount(__p, __d, std::move(__a)) + : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a)) { } template @@ -1278,12 +1280,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _SafeConv<_Yp> reset(_Yp* __p, _Deleter __d) - { __shared_ptr(__p, __d).swap(*this); } + { __shared_ptr(__p, std::move(__d)).swap(*this); } template _SafeConv<_Yp> reset(_Yp* __p, _Deleter __d, _Alloc __a) - { __shared_ptr(__p, __d, std::move(__a)).swap(*this); } + { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); } element_type* get() const noexcept diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/lwg2802.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/lwg2802.cc new file mode 100644 index 00000000000..c69b3787699 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/lwg2802.cc @@ -0,0 +1,51 @@ +// 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 +// . + +// { dg-do compile { target c++11 } } + +#include + +// LWG 2802. shared_ptr constructor requirements for a deleter + +struct D +{ + D() { } + D(D&&) { } + void operator()(int* p) const { delete p; } +}; + +std::allocator a; + +std::shared_ptr s1((int*)nullptr, D()); +std::shared_ptr s2((int*)nullptr, D(), a); +std::shared_ptr s3(nullptr, D()); +std::shared_ptr s4(nullptr, D(), a); + +void test01() +{ + s1.reset((int*)nullptr, D()); + s1.reset((int*)nullptr, D(), a); +} + +struct D2 final +{ + D2() { } + D2(D2&&) { } + void operator()(int* p) const { delete p; } +}; + +std::shared_ptr s5(nullptr, D2());