Constexpr launder and unions
17.6.5 [ptr.launder]
Hubert Tong

Created on 2020-11-07.00:00:00 last changed 1 month ago


Date: 2020-12-15.00:00:00

Notes from the December, 2020 teleconference:

See also library issue 3495.

Date: 2020-11-07.00:00:00

According to 17.6.5 [ptr.launder], referring to std::launder,

An invocation of this function may be used in a core constant expression whenever the value of its argument may be used in a core constant expression.

It is not clear whether this wording is intended to permit an example like

  #include <new>

  struct A { char x; };

  union U {
    A a;
    A a2;

  constexpr A foo() {
    U u = {{42}};
    A *ap = &u.a2;
    return *std::launder(ap);

  extern constexpr A globA = foo();

In particular, is the wording intended to restrict use of std::launder in a constant expression to cases in which the function returns its argument unchanged? As a further example, consider

  #include <new>

  struct A { char x; };
  struct B { A a; };
  struct BytesAndMore {
    unsigned char bytes[sizeof(A)];
    unsigned char more;

  union U {
    BytesAndMore bytes;
    A a;
    B b;

  constexpr B foo() {
    U u;
    A *ap = &u.a;
    B *bp = &u.b;
    u.bytes.more = 0;
    std::launder(ap)->x = 42;
    return *std::launder(bp);

  extern constexpr B globB = foo();
Date User Action Args
2020-12-15 00:00:00adminsetmessages: + msg6285
2020-11-07 00:00:00admincreate