Empty classes, default ctors and const objects?

B

BigMan

Why does an emtpy class require an explicit default ctor in order to
initialize const objects of that type:

class EmptyClass
{
};

class EmptyClass2
{
EmtpyClass2( ) { }
};

int main
(
)
{
EmptyClass const a; // Error: uninitialized const.
EmtpyClass2 const b; // OK.

return 0;
}
 
M

Maxim Yegorushkin

Why does an emtpy class require an explicit default ctor in order to
initialize const objects of that type:

Because the standard requires a constant to have an initializer or its
class a default ctor.
class EmptyClass
{
};

class EmptyClass2
{
EmtpyClass2( ) { }
};

int main
(
)
{
EmptyClass const a; // Error: uninitialized const.

EmptyClass const a = {}; // ok
 
P

Phlip

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( ) { }
};
int main()
{
EmptyClass const a; // Error: uninitialized const.
EmtpyClass2 const b; // OK.

Because of the following linguistic nuance:

assert(0 == int());

Explicitely constructing a primitive with empty parens is defined as the
same as declaring the same primitive at static or global scope. (Facts on
the ground, it has "all bits zero" in it.)

This is so STL containers can construct primitives with reliable values if
you parameterize the container types to them. Given typename _Ty, which
could be anything, an STL container needs _Ty() to construct it and give it
a healthy value. (And recall that the act of collecting the value of an
uninitialized variable is undefined.)

Your compiler is extending the effect to empty classes, despite they have no
collectable bits to initialize.
 
A

Alan Johnson

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.
return 0;
}

-Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,282
Latest member
Xyprime

Latest Threads

Top