From C++11 Standard § 8.5 p6 we have:
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.
The following code should not compile. But it does, both in Coliru and Ideone.
class A{};
int main() {
const A a;
}
Edit:
While trying to understand what's going on here, I ended up with the following code, which compiles (at least it's compliant with the Standard, as A
has a user-provided constructor). But then the following question came to my mind: which Standard clause does guarantee that a.b.j
is initialized with 0 (see the code in Ideone), below?
#include <iostream>
struct B { int j; B(){ std::cout << "B()" << '\n'; } };
struct A
{
struct B b;
int i;
public:
A(): i(1) { std::cout << "A()" << '\n'; }
};
int main() {
const A a;
std::cout << a.b.j << '\n';
std::cout << a.i << '\n';
}
Edit1:
Sorry for the Edit above, but I'm still not used with Unixes. Last week Dietmar Kühl called my attention to the fact that "Most UNIXes start off with zero initialized pages". Therefore, a.b.j is not 0 because of initialization, as I was thinking. As a matter of fact, I've just compiled the code with VS2010, and the result for a.b.j was an unitialized value, as expected. So, the question in Edit should be disregarded.
But I'm curious to know whether clang++ or g++ would also show an error for this second snippet. Thanks.