Title
std::launder and reuse of character buffers
Status
drafting
Section
6.7.2 [intro.object]
Submitter
CA

Created on 2017-02-27.00:00:00 last changed 95 months ago

Messages

Date: 2022-02-18.07:47:23
P0488R0 comment CA 12

The status of the following code should be explicitly indicated in the Standard to avoid surprise:

  #include <new>
  int bar() {
    alignas(int) unsigned char space[sizeof(int)];
    int *pi = new (static_cast<void *>(space)) int;
    *pi = 42;
    return [=]() mutable {
      return   *std::launder(reinterpret_cast<int *>(space)); }();
   }

In particular, it appears that the call to std::launder has undefined behaviour because the captured copy of space is not established to provide storage for an object of type int (sub 6.7.2 [intro.object] paragraph 1). Furthermore, the code has undefined behaviour also because it attempts to access the stored value of the int object through a glvalue of an array type other than one of the ones allowed by sub 7.2.1 [basic.lval] paragraph 8.

History
Date User Action Args
2017-02-27 00:00:00admincreate