Check out the following code for an example:
#include <iostream>
#include <optional>
class A {
public:
A();
~A();
};
std::optional<A> a;
A::A() { std::cout << a.has_value(); }
A::~A() { std::cout << a.has_value(); }
int main() {
a.emplace();
std::cout << a.has_value();
}
I ended up with something similar, and was caught by surprise that a
had no value in the constructor. Which is probably a good thing since the object isn't fully constructed and my design could be improved. But I didn't find any evidence in cppreference about the way it's supposed to behave according to the standard.
So my question is (mainly due to curiosity): What should be the output in the example code above, and is it specified or implementation-defined (or UB)?
Interestingly, the above code prints (spoiler):
010
for GCC and Cland, and011
for MSVC.