473,657 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

_really_ empty classes

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 of size 0, then:

- an object of type foo may lie at the same address as another.
- arrays of foo objects would have size 0, and pointer arithmetic on
array cell would break.
- requirements of the standard explicitly stating objects have
non-zero size would break (1.7.4 and 5.3.3.2, to start with).

(see also links below).

Now, I claim that even considering this, not having provision for
_really_ empty classes is a fiasco of the standard. The so-called
"empty member c++ optimization" (see links) is not a complete cure: it
relies on the availability of a non-empty member on which empty
classes are aggregated. Such a member is not always available. In
template metaprogramming world, one would prize the ability to create
possibly empty classes made of several possibly all empty members.

Extending the language, say via a keyword, to include ``ghost''
classes (by lack of a better name), which really occupy zero storage
when they are empty and present as members of anything would be, IMHO,
a very valuable addition. Implications would include:

- distinct objects of two non-ghost types would still reside at
distinct addresses. In case one of the object is a ghost class, this
would no longer hold.
- pointer arithmetic on ghost class pointers would be illegal.

Does such a thing make sense to anybody ?

Some relevant links include:

http://www.research.att.com/~bs/bs_f...l#sizeof-empty
http://www.cantrip.org/emptyopt.html
http://tinyurl.com/36r26
Jul 22 '05 #1
3 1661
"Emmanuel Thomé" <Em************ *****@loria.fr> wrote
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 of size 0, then:

- an object of type foo may lie at the same address as another.
- arrays of foo objects would have size 0, and pointer arithmetic on
array cell would break.
- requirements of the standard explicitly stating objects have
non-zero size would break (1.7.4 and 5.3.3.2, to start with).
Correct. I'll add that the higher-level reason for these
requirements/restrictions is so that individual instances can have separate
identities.
Now, I claim that even considering this, not having provision for
_really_ empty classes is a FIASCO [my emphasis] of the standard.
And from logic, we take a long leap into the melodramatic.
The so-called "empty member c++ optimization" (see links) is not a
complete cure:
Before looking for a "cure", shouldn't you perhaps establish that there's a
problem to be solved?
relies on the availability of a non-empty member on which empty
classes are aggregated. Such a member is not always available. In
template metaprogramming world, one would prize the ability to create
possibly empty classes made of several possibly all empty members.

Extending the language, say via a keyword, to include ``ghost''
classes (by lack of a better name), which really occupy zero storage
when they are empty and present as members of anything would be, IMHO,
a very valuable addition.
And after proposing a solution to the yet-to-be stated problem, might it not be
appropriate to describe the benefits of the presumed solution?
Implications would include:

- distinct objects of two non-ghost types would still reside at
distinct addresses. In case one of the object is a ghost class, this
would no longer hold.
- pointer arithmetic on ghost class pointers would be illegal.
And in lieu of benefits, we get a list of pitfalls.
Does such a thing make sense to anybody ?


Only if the goal is to mutilate and pervert the language. Was there a point to
this?

Claudio Puviani
Jul 22 '05 #2
In article <c0**********@a rcturus.ciril.f r>,
Emmanuel Thome <Em************ *****@loria.fr> wrote:
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 of size 0, then:

- an object of type foo may lie at the same address as another.
- arrays of foo objects would have size 0, and pointer arithmetic on
array cell would break.
- requirements of the standard explicitly stating objects have
non-zero size would break (1.7.4 and 5.3.3.2, to start with).

(see also links below).

Now, I claim that even considering this, not having provision for
_really_ empty classes is a fiasco of the standard. The so-called
"empty member c++ optimization" (see links) is not a complete cure: it
relies on the availability of a non-empty member on which empty
classes are aggregated. Such a member is not always available. In
template metaprogramming world, one would prize the ability to create
possibly empty classes made of several possibly all empty members.

Extending the language, say via a keyword, to include ``ghost''
classes (by lack of a better name), which really occupy zero storage
when they are empty and present as members of anything would be, IMHO,
a very valuable addition.


Why would it be valuable? What possible benefit could this provide?

Jul 22 '05 #3
Clark Cox wrote:
In article <c0**********@a rcturus.ciril.f r>,
Emmanuel Thome <Em************ *****@loria.fr> wrote:
Extending the language, say via a keyword, to include ``ghost''
classes (by lack of a better name), which really occupy zero storage
when they are empty and present as members of anything would be, IMHO,
a very valuable addition.

Why would it be valuable? What possible benefit could this provide?


Valuable for efficiency. Benefits for code simplicity, compared to
other (existing) solutions. The explanation below is lengthy, sorry.

As explained in http://www.cantrip.org/emptyopt.html, the nonzero
padding leads to some bloat. For efficiency reasons, ways to avoid it
are desired.

Using a template to make the possibly empty class a base class of some
non-empty member is a way to achieve this. boost::compress ed_pair does
so. It is successful in many cases, but restrictions do exist (namely,
you've got to articulate your possibly empty classes around some
non-empty member).

A working code context could be somewhat long and not very
illuminating, I'll only give a sketch (which, unfortunately, does grow
to some length). Consider first the following:

/* objective: behave like an int, without being an int */
template<class T, T v> class fake {
public:
fake() {};
fake(T w) {};
fake(const fake&) {};
~fake() {};
const fake& operator=(const fake&) { return *this;};
T value() { return v; };
};

template<class T, T v> int int_value(fake< T,v> a) { return a.value(); }
int int_value(int a) { return a; }

The idea is to have fake<int, 3> look like an int in some aspects
(really few here :-)), but really be 3 always. Attempts to assign
anything else than 3 to it may be tracked, but we do not want to
forbid such assignments altogether.

As it is, an object of type fake<int,3> occupies nonzero storage, but
that doesn't disturb me too much yet. I'm ok with wasting a couple of
bytes on the stack, not too often.

Now say we've got a handful of properties, which may be either
variable (int) or fixed (fake<int, 3>). These properties are put
together in a template:

template<class U, class Ta, class Tb> class qualified_data {
U raw_data;
Ta p1,p2,p3;// some of type Ta
Tb p4,p5,p6;// some of type Tb
/* lots of interface stuff. */
};

of course we want instantiations of qualified_data to be as small as
possible. As written above, bloat will be significant, and arrays
created from qualified_data will be unsatisfyingly large. A solution
could be to use compressed_pair (abbreviated cp), with the following
statement.
cp<Ta,cp<Ta,cp< Ta,cp<Tb,cp<Tb, cp<Tb, U> > > > > > props;
and then, have accessors for the different fields:
const Ta& p1() const { return props.first(); };
...
const Ta& p5() const { return
props.second(). second().second ().second().fir st(); };

Even if the above statement can be easily trimmed down by some
classical template tricks, the code obtained is error-prone. The
first, naive syntax above makes life easier. Agreed, this is making
life easier for the developer rather than the user, which is a
debatable objective.

If really empty classes were available, the first, much lighter,
syntax would be just as efficient as the first one.

Now, maybe nobody's convinced. I don't want to bother people with the
innards of the project I'm working on. Furthermore, it does turn out
that restating my original problem this way suggests me adding some
syntactic sugar around the cp<cp<cp<...>>> solution above so that I
can get it to be less error-prone. Maybe it'll work out, I'll see. I'm
thinking of statements such as this:

#define TAG_FIELD_1 0x12340001 // and so on...
field_list <
field<Ta, TAG_FIELD_1,
/* ... */
field<Tb, TAG_FIELD_6,
void> > > > > > > many_fields;
const Ta& p5() const { return many_fields.acc ess(TAG_FIELD_5 ); };

I honestly believe that this does not discard my argument about the
interest of really empty classes. So much trickery is exhausting.

E.
Jul 22 '05 #4

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

Similar topics

3
3590
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 be "public interface Value{}" in Java! class IntValue : public Value{ private: int _value; public:
4
1716
by: BigMan | last post by:
Why does an emtpy class require an explicit default ctor in order to initialize const objects of that type: class EmptyClass { }; class EmptyClass2 { EmtpyClass2( ) { }
6
4877
by: free2cric | last post by:
Hi, What will the syntax sound like for checking size of an empty class? what will be the size. Thanks, Cric
5
2321
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following code, sometimes the Person may not have given us one, both, or either of their addresses. The serialize function will still put in a "<BillingAddress />" tag indicating no data. I want it to completely omit the tag if the class is empty, similar...
4
2570
by: avranju | last post by:
Hi, We have a few exception marker classes in a project that are used only to throw exceptions. I mean stuff like this (this may not be such a hot idea for doing exception handling - a simple enum might suffice since we aren't sending any data with the exception - but that's sort of off-topic I suppose): class Whoops{}; class GeeDontKnowHowToDoThat{};
8
5717
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?
9
4086
by: Morgan Cheng | last post by:
Hi, I once worked for java in embedded system. Since the memory is limited in embedded system, there are some guidelines for programming. One of them is "Don't create too many classes, because each class takes up at least 200 bytes". The 200 bytes are byte-code, not size of a class instance. Since Java and C# is so similar, I am wandering how many bytes will it take for a C# class after compiled into IL code. If it also consumes
7
34821
by: kumar.senthil | last post by:
Hi, I'm using XmlSerializer to create an object from the XML string. I would like to know whether I can get a null value for an empty XML element. Actually the XmlSerializer assigns "" (empty string) for a string if the corresponding element is missing in the XML. Input XML: <DemoClass><A>some value</A><B></B><DemoClass>
21
2955
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.