BigMan said:
Why does an emtpy class require an explicit default ctor in order to
initialize const objects of that type:
class EmptyClass
{
};
class EmptyClass2
{
EmtpyClass2( ) { }
EmptyClass2, not EmtpyClass2. Also, did you mean for this to be private?
};
int main
(
)
{
EmptyClass const a; // Error: uninitialized const.
From the standard, clause 8.5.9:
If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the
object shall be default-initialized; _if the object is of
const-qualified type, the underlying class type shall have a
user-declared default constructor_. Otherwise, if no initializer is
specified for a nonstatic object, the object and its subobjects, if any,
have an indeterminate initial value90); if the object or any of its
subobjects are of const-qualified type, the program is ill-formed.
(emphasis mine)
The only answer I can give is "because the standard says so". There is
no reason that is apparently obviouis to me why this restriction exists.
Perhaps someone with more insight can enlighten us both.
EmtpyClass2 const b; // OK.
EmptyClass2, not EmtpyClass2. This is an error, EmptyClass2 has a
private default constructor.
-Alan