Orient Orient - 1 year ago 44
C++ Question

trivially default constructible std::optional and std::variant

Is it permitable to design

) in such a way, that for trivially default constructible type
std::optional< T >
is also trivially default constructible?

The same question regading
and its integral discriminator.

My own answer is: "No, it cannot be designed in this way, because value of its integral discriminator obtained during default initialization will be indeterminate if the object has automatic storage duration or if it is
-ed from non-zero-initialized storage." Requirement to the user to do value-initialization every time is not allowed on my mind.

Answer Source

Your answer is correct: you cannot. The specification requires that its "initialized flag" is set to false upon default construction.