Skip to content

[intro.object] Example for p10 #6429

@eisenwave

Description

@eisenwave

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 objects

std::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 behavior

std::memmove(&x, &x, sizeof(x));                 // begin the lifetime of one or multiple objects in the storage of x
std::launder(reinterpret_cast<short*>(&x));      // created object at &x must be of type short int
x = 0;                                           // undefined behavior

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions