Skip to content

P2434 [basic.types.general] [basic.compound] Pointer values are not determined by bits in the object representation #931

Description

@languagelawyer

Full name of submitter: Andrey Erokhin

Reference (section label): [basic.types.general] [basic.compound]

Link to reflector thread (if any): -

Issue description:

(Wanted to file the issue for many years)

[basic.types.general] says:

For trivially copyable types, the value representation is a set of bits in the object representation that determines [emphasis mine] a value, which is one discrete element of an implementation-defined set of values.

For pointers, bits in their representation can't determine the value.

A simple example:

int a[2][2];

int* p1 = &a[0][2];
int* p2 = &a[1][0];

p1 and p2 usually have the same bits in their representation, but they are different values.

And, ofc, one can produce arbitrary number of pointer values with the same representation, but different values by replacing an object:

int i;

int* p1 = &i;
std::byte buf[sizeof(p1)];
std::memcpy(buf, &p1, sizeof(p1)); // save the value

int* p2 = ::new(&i) int; // rebinds p1 (is it a write to p1?)
std::memcpy(&p1, buf, sizeof(p1)); // restore p1 value

p1 and p2 point to different objects (thus have different values), but bits in their representation are usually the same

Suggested resolution:

Exclude pointers from trivially copyable types

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions