C++17 std::launder and aliasing

Jonathan Wakely jwakely@redhat.com
Mon Oct 24 11:55:00 GMT 2016


On 24/10/16 13:47 +0200, 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.
>
>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?

Yes, I think that's UB according to 3.8 [basic.life] p8. The name 'a'
cannot be used to manipulate the new object at that location, because
the second bullet of p8 is not true.

>  int e = std::launder(&a)->f();
>  if(b != 2 || c != 1 || d != 1 || f != 1)
>    std::abort();
>}
>
>	Jakub



More information about the Libstdc++ mailing list