473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Empty classes, default ctors and const objects?

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;
}

Jul 23 '05 #1
4 1700
On Sat, 25 Jun 2005 23:39:28 +0400, BigMan <Bi****@abv.bg> wrote:
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
EmtpyClass2 const b; // OK.
return 0;
}


--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #2
BigMan wrote:
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.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #3
BigMan wrote:
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
Jul 23 '05 #4
1. EmptyClass2 was meant to have a public default ctor.
2. Thanks for the citation.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Suzanne Vogel | last post by:
** Is there a way (C++ syntax) to specify that a pointer should refer to an object that derives from *two* specific classes? eg, Suppose I have two classes, 'A' and 'B', and I want to specify...
3
by: Emmanuel Thomé | last post by:
This is a comment aside the empty class behavior FAQ. I understand there are a fair number of reasons which make empty classes have non-zero size (except as base classes). If ``class foo'' were...
3
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would...
17
by: highli | last post by:
When a non-default constructor provided in a class, the default constructor is not available anymore. In what cases shall a default constructor be defined explicitly? Specifically, in the...
8
by: meendar | last post by:
what will a object of an Empty class( contain nothing), do on default.What are all the default methods it calls. what is the use of creating the object for an empty class?
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
7
by: Jim | last post by:
Hi, I'm trying to build a simple data table for all my classes to use. I don't want to use a vector. The data is stored in class freqoffset class freqoffset { public:...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.