How does default initialization work in C++11 if the default constructor is explicit? For example:
#include <iostream>
struct Foo {
int x;
explicit Foo(int y = 7) : x{y} {}
}
int main() {
Foo foo;
std::cout << foo.x << std::endl;
}
In main, the variable foo is default initialized. Based on my understanding, this will call a default constructor, if one exists. Otherwise, no initialization occurs, foo contains indeterminate values, and printing foo.x is undefined behavior.
There is a default constructor for Foo, but it is explicit. Is that constructor guaranteed to be called, or is the last line of the program undefined behavior?
Foo foo;? You have explicitly told the compiler the type you want to use and the name you want to give the object, so why would that not be explicit?