From: Jonathan Wakely Date: Fri, 26 May 2023 20:33:58 +0000 (+0100) Subject: libstdc++: Add missing noexcept to std::scoped_allocator_adaptor X-Git-Tag: releases/gcc-12.4.0~324 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65704fa293ca9d5f9186aa09037fc2869492973b;p=thirdparty%2Fgcc.git libstdc++: Add missing noexcept to std::scoped_allocator_adaptor The standard requires these constructors and accessors to be noexcept. libstdc++-v3/ChangeLog: * include/std/scoped_allocator (scoped_allocator_adaptor): Add noexcept to all constructors except the default constructor. (scoped_allocator_adaptor::inner_allocator): Add noexcept. (scoped_allocator_adaptor::outer_allocator): Likewise. * testsuite/20_util/scoped_allocator/noexcept.cc: New test. (cherry picked from commit b960c253e988c68ed3f3829125bc267bdf169356) --- diff --git a/libstdc++-v3/include/std/scoped_allocator b/libstdc++-v3/include/std/scoped_allocator index 8ad4c90b694d..dd00e8efdbbf 100644 --- a/libstdc++-v3/include/std/scoped_allocator +++ b/libstdc++-v3/include/std/scoped_allocator @@ -66,7 +66,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct __outermost_type { using type = _Alloc; - static type& _S_outermost(_Alloc& __a) { return __a; } + static type& _S_outermost(_Alloc& __a) noexcept { return __a; } }; template @@ -80,7 +80,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION >; static typename __base::type& - _S_outermost(_Alloc& __a) + _S_outermost(_Alloc& __a) noexcept { return __base::_S_outermost(__a.outer_allocator()); } }; @@ -105,11 +105,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __inner_type_impl& operator=(__inner_type_impl&&) = default; template - __inner_type_impl(const __inner_type_impl<_Alloc>& __other) + __inner_type_impl(const __inner_type_impl<_Alloc>& __other) noexcept { } template - __inner_type_impl(__inner_type_impl<_Alloc>&& __other) + __inner_type_impl(__inner_type_impl<_Alloc>&& __other) noexcept { } __type& @@ -138,16 +138,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __inner_type_impl& operator=(__inner_type_impl&&) = default; template - __inner_type_impl(const __inner_type_impl<_Allocs...>& __other) + __inner_type_impl(const __inner_type_impl<_Allocs...>& __other) noexcept : _M_inner(__other._M_inner) { } template - __inner_type_impl(__inner_type_impl<_Allocs...>&& __other) + __inner_type_impl(__inner_type_impl<_Allocs...>&& __other) noexcept : _M_inner(std::move(__other._M_inner)) { } template explicit - __inner_type_impl(_Args&&... __args) + __inner_type_impl(_Args&&... __args) noexcept : _M_inner(std::forward<_Args>(__args)...) { } __type& @@ -308,31 +308,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> scoped_allocator_adaptor(_Outer2&& __outer, - const _InnerAllocs&... __inner) + const _InnerAllocs&... __inner) noexcept : _OuterAlloc(std::forward<_Outer2>(__outer)), _M_inner(__inner...) { } - scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) + scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) noexcept : _OuterAlloc(__other.outer_allocator()), _M_inner(__other._M_inner) { } - scoped_allocator_adaptor(scoped_allocator_adaptor&& __other) + scoped_allocator_adaptor(scoped_allocator_adaptor&& __other) noexcept : _OuterAlloc(std::move(__other.outer_allocator())), _M_inner(std::move(__other._M_inner)) { } template> scoped_allocator_adaptor( - const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other) + const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other + ) noexcept : _OuterAlloc(__other.outer_allocator()), _M_inner(__other._M_inner) { } template> scoped_allocator_adaptor( - scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other) + scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other) noexcept : _OuterAlloc(std::move(__other.outer_allocator())), _M_inner(std::move(__other._M_inner)) { } @@ -343,25 +344,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default; - inner_allocator_type& inner_allocator() noexcept + inner_allocator_type& + inner_allocator() noexcept { return _M_inner._M_get(this); } - const inner_allocator_type& inner_allocator() const noexcept + const inner_allocator_type& + inner_allocator() const noexcept { return _M_inner._M_get(this); } - outer_allocator_type& outer_allocator() noexcept + outer_allocator_type& + outer_allocator() noexcept { return static_cast<_OuterAlloc&>(*this); } - const outer_allocator_type& outer_allocator() const noexcept + const outer_allocator_type& + outer_allocator() const noexcept { return static_cast(*this); } - _GLIBCXX_NODISCARD pointer allocate(size_type __n) + _GLIBCXX_NODISCARD pointer + allocate(size_type __n) { return __traits::allocate(outer_allocator(), __n); } - _GLIBCXX_NODISCARD pointer allocate(size_type __n, const_void_pointer __hint) + _GLIBCXX_NODISCARD pointer + allocate(size_type __n, const_void_pointer __hint) { return __traits::allocate(outer_allocator(), __n, __hint); } - void deallocate(pointer __p, size_type __n) + void deallocate(pointer __p, size_type __n) noexcept { return __traits::deallocate(outer_allocator(), __p, __n); } size_type max_size() const diff --git a/libstdc++-v3/testsuite/20_util/scoped_allocator/noexcept.cc b/libstdc++-v3/testsuite/20_util/scoped_allocator/noexcept.cc new file mode 100644 index 000000000000..16992968d3b9 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/scoped_allocator/noexcept.cc @@ -0,0 +1,47 @@ +// { dg-do compile { target c++11 } } + +#include + +template +struct Alloc +{ + using value_type = T; + + Alloc() noexcept(false) { } + Alloc(const Alloc&) noexcept(false) { } + template Alloc(const Alloc&) noexcept { } + + T* allocate(std::size_t); + void deallocate(T*, std::size_t); + + bool operator==(const Alloc&) const noexcept(false) { return true; } + bool operator!=(const Alloc&) const noexcept(false) { return false; } +}; + +using A1 = std::allocator; +using A2 = std::allocator; +using Ax = Alloc; +using SA1 = std::scoped_allocator_adaptor; +using SA2 = std::scoped_allocator_adaptor; +static_assert( std::is_default_constructible::value + && ! std::is_nothrow_default_constructible::value, + "default constructor is potentially-throwing" ); +static_assert( std::is_nothrow_constructible::value, + "multi-arg constructor is non-throwing" ); +static_assert( std::is_nothrow_copy_constructible::value, + "copy constructor is non-throwing" ); +static_assert( std::is_nothrow_move_constructible::value, + "move constructor is non-throwing" ); +static_assert( std::is_nothrow_constructible::value, + "converting copy constructor is non-throwing" ); +static_assert( std::is_nothrow_constructible::value, + "converting move constructor is non-throwing" ); + +static_assert( noexcept(std::declval().inner_allocator()), + "inner_allocator() is non-throwing" ); +static_assert( noexcept(std::declval().inner_allocator()), + "inner_allocator() const is non-throwing" ); +static_assert( noexcept(std::declval().outer_allocator()), + "outer_allocator() is non-throwing" ); +static_assert( noexcept(std::declval().outer_allocator()), + "outer_allocator() const is non-throwing" );