I guess I have two questions:
1. Does std::construct_at change the active member of a union?
My use case for 2 is providing storage for an uninitialized array in constexpr context, e.g., for a statically sized constexpr “vector<T, N>”, that is considered contiguous storage and is not dynamically allocated.
Given some hypothetical struct and union-based constexpr vector storage:
struct Foo
{
constexpr explicit Foo(int) {}
};
template <class T, std::size_t N>
struct Storage
{
Foo storage[N];
constexpr Foo() {} // no active member
};
I believe
https://eel.is/c++draft/class.union#general-6 implies
it is possible to start the lifetime of the data array without starting the lifetime of every element in the data array via an **assignment** to an individual element in data.
constexpr void f()
{
Storage<Foo, 2> u;
u.data[1] = Foo {0};
}
However given a hypothetical struct without assignment it is not possible.
struct Bar
{
constexpr explicit Bar(int) {}
constexpr Bar(Bar const&) = delete;
constexpr Bar(Bar&&) = delete;
};
constexpr void g()
{
Storage<Bar, 2> u;
u.data[1] = Bar {0}; // ERROR: use of deleted function
constexpr void h()
{
Storage<Bar, 2> u;
std::construct_at(&u.data[1], 0);
}
Luke
----
Luke D’Alessandro, Ph.D.
Department of Intelligent Systems Engineering
Indiana University Bloomington
ldalessa@iu.edu