C++ Logo


Advanced search

Re: [External] Constexpr union array activation

From: D'Alessandro, Luke K <ldalessa_at_[hidden]>
Date: Thu, 9 Sep 2021 15:07:14 +0000

> On Sep 8, 2021, at 9:58 PM, Jason McKesson via Std-Proposals <std-proposals_at_lists.isocpp.org> wrote:
> This message was sent from a non-IU address. Please exercise caution when clicking links or opening attachments from external sources.
> -------
> On Wed, Sep 8, 2021 at 2:29 PM D'Alessandro, Luke K via Std-Proposals
> <std-proposals_at_lists.isocpp.org> wrote:
>> I guess I have two questions:
>> 1. Does std::construct_at change the active member of a union?
> As stated in [class.union]/2, "a non-static data member is active if
> its name refers to an object whose lifetime has begun and has not
> ended." `construct_at` can begin the lifetime of objects (which can
> also cause the lifetime of other overlapping objects to end).
> Therefore, it can change the active union member.

Oh that’s great! Relying on the note seemed like a sketchy proposition.

>> 2. If 1, can std::construct_at benefit from the logic in https://eel.is/c++draft/class.union#general-6 that begins the lifetime of a nominated member?
> No. That paragraph applies to the explicit use of the assignment operator.

Okay, so it’s not really possible to directly interact with the array in the way that I wanted to. Johel Ernesto Guerrero Peña pointed out that I should be able to indirectly start the lifetime of an array for non-assignable types as:

    struct A
        Int x;
        NonAssignable data[1];

    union Storage
        A a;

    constexpr void f()
        Storage s;
        s.x = 0; // <-- starts the lifetime of both s.x and s.data[]
        std::construct_at(&s.data[0], ….);

Thank you for the help.


> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-09-09 10:09:05