This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4064. Clarify that std::launder is not needed when using the result of std::memcpy

Section: 23.5.3 [cstring.syn] Status: New Submitter: Jan Schultke Opened: 2024-04-05 Last modified: 2024-04-11

Priority: Not Prioritized

View all issues with New status.

Discussion:

int x = 0;
alignas(int) std::byte y[sizeof(int)];
int z = *static_cast<int*>(std::memcpy(y, &x, sizeof(int)));

This example should be well-defined, even without the use of std::launder. std::memcpy implicitly creates an int inside y, and https://www.iso-9899.info/n3047.html#7.26.2.1p3 states that

The memcpy function returns the value of [the destination operand].

In conjunction with 23.5.3 [cstring.syn] p3, this presumably means that std::memcpy returns a pointer to the (first) implicitly-created object, and no use of std::launder is necessary.

The wording should be clarified to clearly support this interpretation or reject it.

Proposed resolution:

This wording is relative to N4971.

  1. Modify 23.5.3 [cstring.syn] as indicated:

    -3- The functions memcpy and memmove are signal-safe (17.13.5 [support.signal]). Both functions implicitly create objects (6.7.2 [intro.object]) in the destination region of storage immediately prior to copying the sequence of characters to the destination. Both functions return a pointer to a suitable created object.