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

Jonathan Wakely jwakely@redhat.com
Mon Jun 28 14:19:09 GMT 2021


On Thu, 24 Jun 2021 at 22:11, Tim Song wrote:
>
> That example violates http://eel.is/c++draft/unique.ptr.runtime.general#3

Even though it's undefined I committed a workaround to allow it,
because it breaks LLVM:
https://gcc.gnu.org/pipermail/libstdc++/2021-June/052851.html
(I forgot to send that mail as a reply to this thread, sorry).

> On Thu, Jun 24, 2021 at 1:55 PM Patrick Palka via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > 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