C++17 std::launder and aliasing

Richard Biener rguenther@suse.de
Mon Oct 24 11:53:00 GMT 2016


On Mon, 24 Oct 2016, Jakub Jelinek wrote:

> On Mon, Oct 24, 2016 at 01:38:13PM +0200, Richard Biener wrote:
> > Certainly a possibility - though points-to information is _not_ affected
> > by launder semantics.  std::launder only is a memory optimization
> > barrier for aliasing accesses (I believe even TBAA is valid as it
> > constrains the types that can be instantiated at the place).  For that
> > to work you'd have to instead make it have a VDEF and (optionally)
> > add special code to the stmt_may_use/clobber alias helpers.
> 
> I guess the question is if std::launder affects just the returned pointer,
> or something else too.

I think it doesn't affect the returned pointer but the object
pointed to by it.

> struct A {
>   virtual int f();
>   virtual int g() { return 2; }
> };
> struct B : A {
>   virtual int f() { new (this) A; return 1; }
>   virtual int g() { return 1; }  
> };
> int A::f() { new (this) B; return 2; }
> static_assert(sizeof(B) == sizeof(A), "");
> 
> int main() {
>   A a;
>   int b = a.f();
>   int c = std::launder(&a)->g();
>   int d = a.g(); // Is this UB?

I don't think so?

>   int e = std::launder(&a)->f();

and the launder here is not necessary either?

>   if(b != 2 || c != 1 || d != 1 || f != 1)
>     std::abort();
> }



More information about the Libstdc++ mailing list