Skip to content

[class.union.general] Unclear selection of variant member activated by placement new #746

@frederick-vs-ja

Description

@frederick-vs-ja

Full name of submitter (unless configured in github; will be published with the issue): Jiang An

Reference (section label): [class.union.general], [expr.static.cast]

Link to reflector thread (if any):

Issue description:

May be a part of CWG2675. Consider the following example:

#include <new>

union U {
  int a;
  int b;
};

static_assert(sizeof(U) == sizeof(int)); // passes on most platforms

int main() {
  U u; // no active member until C++26; u.a is active since C++26
  ::new (&u) int{};                         // #1
  ::new (reinterpret_cast<int*>(&u)) int{}; // #2
  ::new (&u.a) int{};                       // #3
}

On most platforms, u, u.a, and u.b occupy the same storage. So according to [intro.object] p2, all of #1, #2, #3 should create a subobject of u. But it is unclear which subobject is replaced.

It seems that we should at least guarantee #3 to replace u.a, which is necessary for std::variant and std::expected.

For #2, it's even unclear whether reinterpret_cast<int*>(&u) points to u.a or u.b. I guess it's better to introduce angelic indeterminism for such selections, i.e. to select an unspecified subobject in a way maximizing defined prefix of the execution (as suggested by P1839 and updated by CWG3039).

Suggested resolution:

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