[committed] libstdc++: Implement LWG 2762 for std::unique_ptr::operator*

Patrick Palka ppalka@redhat.com
Thu Jun 24 18:53:55 GMT 2021


On Thu, 24 Jun 2021, Jonathan Wakely via Libstdc++ wrote:

> The LWG issue proposes to add a conditional noexcept-specifier to
> std::unique_ptr's dereference operator. The issue is currently in
> Tentatively Ready status, but even if it isn't voted into the draft, we
> can do it as a conforming extensions. This commit also adds a similar
> noexcept-specifier to operator[] for the unique_ptr<T[], D> partial
> specialization.

The conditional noexcept added to unique_ptr<T[]>::operator[] seems to break
the case where T is complete only after the fact:

  struct T;
  extern std::unique_ptr<T[]> p;
  auto& t = p[1];
  struct T { };

/include/c++/12.0.0/bits/unique_ptr.h: In instantiation of ‘typename std::add_lvalue_reference<_Tp>::type std::unique_ptr<_Tp [], _Dp>::operator[](std::size_t) co
nst [with _Tp = A; _Dp = std::default_delete<A []>; typename std::add_lvalue_reference<_Tp>::type = A&; std::size_t = long unsigned int]’:
testcase.cc:5:14:   required from here
/include/c++/12.0.0/bits/unique_ptr.h:658:48: error: invalid use of incomplete type ‘struct A’
  658 |       noexcept(noexcept(std::declval<pointer>()[std::declval<size_t&>()]))
      |                         ~~~~~~~~~~~~~~~~~~~~~~~^
testcase.cc:3:8: note: forward declaration of ‘struct A’
    3 | struct A;
      |        ^

> 
> Also ensure that all dereference operators for shared_ptr are noexcept,
> and adds tests for the std::optional accessors modified by the issue,
> which were already noexcept in our implementation.
> 
> Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
> 
> libstdc++-v3/ChangeLog:
> 
> 	* include/bits/shared_ptr_base.h (__shared_ptr_access::operator[]):
> 	Add noexcept.
> 	* include/bits/unique_ptr.h (unique_ptr::operator*): Add
> 	conditional noexcept as per LWG 2762.
> 	* testsuite/20_util/shared_ptr/observers/array.cc: Check that
> 	dereferencing cannot throw.
> 	* testsuite/20_util/shared_ptr/observers/get.cc: Likewise.
> 	* testsuite/20_util/optional/observers/lwg2762.cc: New test.
> 	* testsuite/20_util/unique_ptr/lwg2762.cc: New test.
> 
> Tested powerpc64le-linux. Committed to trunk.
> 
> 


More information about the Gcc-patches mailing list