This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: C++17 std::launder and aliasing
On 21 October 2016 at 21:07, Jakub Jelinek <jakub@redhat.com> wrote:
> Seems that testcase is devirtualization related.
> With -O3 -fno-devirtualize we return 3 (but still no calls).
> With asm ("" : "+g" (p)); before return p; in launder we don't devirtualize
> it any more and return 3 in the end. Is the testcase only valid
> with std::launder and not valid otherwise (I hope so, otherwise we are in
> big trouble with devirtualization)?
Seems so. If the compiler assumes that the A object is always an A, we
will not call
the B::f that turns the B back into A, in which case the A object now
contains a B and
we would attempt to destroy an A, not a B. With launder, it's valid,
since the storage
location of the automatic A is switched from containing an A to
containing a B and back,
and none of the destructors of the original objects the storage of
which is reused are
called.
When a launder is used, devirtualization needs to be disabled until we
again know what
the dynamic type is. As Richard explained, "launder must act as a
barrier for devirtualization
information for the resulting poitner value, just like it must do so
for constant propagation
for const and reference members".