[committed] libstdc++: Simplify std::shared_ptr construction from std::weak_ptr
Stephan Bergmann
sbergman@redhat.com
Mon Oct 26 07:07:19 GMT 2020
On 21/10/2020 22:14, Jonathan Wakely via Gcc-patches wrote:
> The _M_add_ref_lock() and _M_add_ref_lock_nothrow() members of
> _Sp_counted_base are very similar, except that the former throws an
> exception when the use count is zero and the latter returns false. The
> former (and its callers) can be implemented in terms of the latter.
> This results in a small reduction in code size, because throwing an
> exception now only happens in one place.
>
> libstdc++-v3/ChangeLog:
>
> * include/bits/shared_ptr.h (shared_ptr(const weak_ptr&, nothrow_t)):
> Add noexcept.
> * include/bits/shared_ptr_base.h (_Sp_counted_base::_M_add_ref_lock):
> Remove specializations and just call _M_add_ref_lock_nothrow.
> (__shared_count, __shared_ptr): Use nullptr for null pointer
> constants.
> (__shared_count(const __weak_count&)): Use _M_add_ref_lock_nothrow
> instead of _M_add_ref_lock.
> (__shared_count(const __weak_count&, nothrow_t)): Add noexcept.
> (__shared_ptr::operator bool()): Add noexcept.
> (__shared_ptr(const __weak_ptr&, nothrow_t)): Add noexcept.
>
> Tested powerpc64le-linux. Committed to trunk.
Clang now complains about
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:230:5: error: '_M_add_ref_lock_nothrow' is missing exception specification 'noexcept'
> _M_add_ref_lock_nothrow()
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:158:7: note: previous declaration is here
> _M_add_ref_lock_nothrow() noexcept;
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:241:5: error: '_M_add_ref_lock_nothrow' is missing exception specification 'noexcept'
> _M_add_ref_lock_nothrow()
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:158:7: note: previous declaration is here
> _M_add_ref_lock_nothrow() noexcept;
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:255:5: error: '_M_add_ref_lock_nothrow' is missing exception specification 'noexcept'
> _M_add_ref_lock_nothrow()
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:158:7: note: previous declaration is here
> _M_add_ref_lock_nothrow() noexcept;
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:876:5: error: exception specification in declaration does not match previous declaration
> __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
> ^
> ~gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../include/c++/11.0.0/bits/shared_ptr_base.h:696:16: note: previous declaration is here
> explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
> ^
> 4 errors generated.
which would be fixed with
> diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
> index a9e1c9bb1d5..10c9c831411 100644
> --- a/libstdc++-v3/include/bits/shared_ptr_base.h
> +++ b/libstdc++-v3/include/bits/shared_ptr_base.h
> @@ -227,7 +227,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> template<>
> inline bool
> _Sp_counted_base<_S_single>::
> - _M_add_ref_lock_nothrow()
> + _M_add_ref_lock_nothrow() noexcept
> {
> if (_M_use_count == 0)
> return false;
> @@ -238,7 +238,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> template<>
> inline bool
> _Sp_counted_base<_S_mutex>::
> - _M_add_ref_lock_nothrow()
> + _M_add_ref_lock_nothrow() noexcept
> {
> __gnu_cxx::__scoped_lock sentry(*this);
> if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
> @@ -252,7 +252,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> template<>
> inline bool
> _Sp_counted_base<_S_atomic>::
> - _M_add_ref_lock_nothrow()
> + _M_add_ref_lock_nothrow() noexcept
> {
> // Perform lock-free add-if-not-zero operation.
> _Atomic_word __count = _M_get_use_count();
> @@ -693,7 +693,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> explicit __shared_count(const __weak_count<_Lp>& __r);
>
> // Does not throw if __r._M_get_use_count() == 0, caller must check.
> - explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
> + explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) noexcept;
>
> ~__shared_count() noexcept
> {
More information about the Libstdc++
mailing list