Is it permitable to design std::optional
(currently std::experimental::optional
) in such a way, that for trivially default constructible type T
corresponding std::optional< T >
is also trivially default constructible?
The same question regading std::variant
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 reinterpret_cast
-ed from non-zero-initialized storage." Requirement to the user to do value-initialization every time is not allowed on my mind.
optional
orvariant
) should be as generic as possible, isn't it? I attempt to designvariant
for constexpr. And currently it has above ability. But I think "maybe I should to deny it?". – Kriegervariant
. Default initialized objects has different behaviour in compile time (constexpr) and runtime. Then I read about intiializations. And found out, that there is UB to use my default intiializedvariant
on the stack. – Krieger