You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think p10 could need an example, ideally on which highlights the idiom of using std::launder to force the creation of one particular type of object.
int x;
new (storage) std::byte[sizeof(x)]; // implicitly begin the lifetime of objects in x
*reinterpret_cast<char*>(&x); // unspecified whether placement new created// an int object inside x, float, char[], or any other objectsstd::memmove(&x, &x, sizeof(x)); // transparently replace ([basic.life]) x with another object
x = 0; // the new object must be of type (no cv) int because // only that may give the program well-defined behaviorstd::memmove(&x, &x, sizeof(x)); // begin the lifetime of one or multiple objects in the storage of xstd::launder(reinterpret_cast<short*>(&x)); // created object at &x must be of type short int
x = 0; // undefined behavior
The text was updated successfully, but these errors were encountered:
I'm afraid that the currently proposed example might be an anti-pattern. Note that it's almost always (if not always) a bad practice to ignore the return of std::launder, and std::launder is marked with [[nodiscard]] (since C++20) as a result.
Perhaps it would be better to write something like *std::launder(reinterpret_cast<short*>(&x)) = 0 instead of a single std::launder call.
On ther other hand, as said in [intro.object]/10, the std::memmove call may recreate or merely rewrite the int object. I'm not sure whether it would be helpful to mention such indeterminism in comments.
I think p10 could need an example, ideally on which highlights the idiom of using
std::launder
to force the creation of one particular type of object.The text was updated successfully, but these errors were encountered: