 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthew Wilson Guest
|
Posted: Sun Aug 24, 2003 11:43 pm Post subject: Empty Derived Optimisation (EDO) ??? |
|
|
Sorry if this touches on threads it's been beyond me to search, but I
was wondering what (a) the language, and (b) implementations have to
say about the following:
template <typename T>
class empty_derived
: public T
{};
class Simple
{};
class Poly
{
public:
virtual ~Poly()
{}
};
int main()
{
bool b1 = (sizeof(Simple) == simple(empty_derived<Simple>));
bool b2 = (sizeof(Poly) == simple(empty_derived<Poly>));
return 0;
}
It is my general experience with Borland, Comeau, CodeWarrior, Digital
Mars, GCC, Intel, Visual C++ and Watcom, that only Borland fails to
have both b1 and b2 true. In other words, just about all compilers
(well Win32 one's anyway) implement what I'm clumsily terming Empty
Derived Optimisation.
So my question is, is this behaviour a nice feature of the
implementations (and not a hard one to achieve, one would think), or
is it a recognised part of the language that all compilers *should*
provide?
Thanks in advance
Matthew Wilson
STLSoft moderator and C++ monomaniac
mailto:admin (AT) stlsoft (DOT) org
mailto:stlsoft (AT) hotmail (DOT) com
http://www.stlsoft.org
news://news.digitalmars.com/c++.stlsoft
"If I'm curt with you it's because time is a factor. I think fast, I
talk fast, and I need you guys to act fast" -- Mr Wolf
----------------------------------------------------------------------------
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Mon Aug 25, 2003 9:15 am Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
[email]stlsoft (AT) hotmail (DOT) com[/email] (Matthew Wilson) writes:
| Quote: | Sorry if this touches on threads it's been beyond me to search, but I
was wondering what (a) the language, and (b) implementations have to
say about the following:
template <typename T>n
class empty_derived
: public T
{};
class Simple
{};
class Poly
{
public:
virtual ~Poly()
{}
};
int main()
{
bool b1 = (sizeof(Simple) == simple(empty_derived<Simple>));
bool b2 = (sizeof(Poly) == simple(empty_derived<Poly>));
|
Surely you mean:
bool b1 = (sizeof(Simple) == sizeof(empty_derived<Simple>));
bool b2 = (sizeof(Poly) == sizeof(empty_derived<Poly>));
instead? (note sizeof and not simple.)
| Quote: | return 0;
}
[snip]
So my question is, is this behaviour a nice feature of the
implementations (and not a hard one to achieve, one would think), or
is it a recognised part of the language that all compilers *should*
provide?
[snip] |
I think it's an implementator's tradeoff.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David B. Held Guest
|
Posted: Mon Aug 25, 2003 9:16 am Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
"Matthew Wilson" <stlsoft (AT) hotmail (DOT) com> wrote
| Quote: | [...]
It is my general experience with Borland, Comeau, CodeWarrior,
Digital Mars, GCC, Intel, Visual C++ and Watcom, that only
Borland fails to have both b1 and b2 true. In other words, just about
all compilers (well Win32 one's anyway) implement what I'm
clumsily terming Empty Derived Optimisation.
|
It's called "Empty Base Optimization" (EBO), and the standard allows
it, but does not require it (as is the case with most, if not all
optimizations in C++). In fact bcc *does* implement EBO, but not
consistently. You might have failed to set a compiler switch to turn
it on.
| Quote: | So my question is, is this behaviour a nice feature of the
implementations (and not a hard one to achieve, one would think),
or is it a recognised part of the language that all compilers *should*
provide?
|
In an ideal world, all compilers would provide it; but in an ideal
world, all compilers would be fast, conforming, and produce small,
fast executables. And while single-inheritance EBO seems like it
would be easy to implement, the issues get tricky with multiple
inheritance. For an example of EBO explicitly used in a library,
check out boost::compressed_pair<>.
Dave
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Arnold Hendriks Guest
|
Posted: Mon Aug 25, 2003 9:18 am Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
"Matthew Wilson" <stlsoft (AT) hotmail (DOT) com> wrote
| Quote: | It is my general experience with Borland, Comeau, CodeWarrior, Digital
Mars, GCC, Intel, Visual C++ and Watcom, that only Borland fails to
have both b1 and b2 true. In other words, just about all compilers
(well Win32 one's anyway) implement what I'm clumsily terming Empty
Derived Optimisation.
|
Isn't this what I've seen more often called "empty base class optimisation"
? Borland's compiler supports it as well, although it's controlled through a
switch (and it's buggy in BCB5 if multiple inheritance is involved) -
perhaps you didn't enable it?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Torjo Guest
|
Posted: Tue Aug 26, 2003 8:12 am Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
| Quote: | int main()
{
bool b1 = (sizeof(Simple) == simple(empty_derived<Simple>));
bool b2 = (sizeof(Poly) == simple(empty_derived<Poly>));
return 0;
}
|
sizeof instead of simple.
| Quote: |
It is my general experience with Borland, Comeau, CodeWarrior, Digital
Mars, GCC, Intel, Visual C++ and Watcom, that only Borland fails to
have both b1 and b2 true. In other words, just about all compilers
(well Win32 one's anyway) implement what I'm clumsily terming Empty
Derived Optimisation.
So my question is, is this behaviour a nice feature of the
implementations (and not a hard one to achieve, one would think), or
is it a recognised part of the language that all compilers *should*
provide?
|
It's not a requirement, the C++ Standard allows it though.
It's called Empty Base class optimization.
In generic programming, it's *quite* useful.
That's why boost::compressed_pair exists.
Best,
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthew Wilson Guest
|
Posted: Tue Aug 26, 2003 8:19 am Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
| Quote: | It's called "Empty Base Optimization" (EBO), and the standard allows
it, but does not require it (as is the case with most, if not all
optimizations in C++). In fact bcc *does* implement EBO, but not
consistently. You might have failed to set a compiler switch to turn
it on.
|
AFAIUI EBO is not what's going on here. Maybe my example gave the
wrong impression. I'll rephrase
template <typename T>
class empty_derived
: public T
{
// No per-instance (non-static) member variables
// No explicitly virtual methods, i.e. no use of virtual keyword
};
class Simple
{
int i;
};
class Poly
{
public:
virtual ~Poly()
{}
int i;
};
int main()
{
bool b1 = (sizeof(Simple) == sizeof(empty_derived<Simple>));
bool b2 = (sizeof(Poly) == sizeof(empty_derived<Poly>));
return 0;
}
EBO would only apply if the base classes Simple and Poly were empty,
which they're not. It's the derived class empty_derived that is empty
hence E*D*O.
(btw, I'm not allied to the term. It's just a place-holder until
someone enlightens me.)
Matthew Wilson
STLSoft moderator and C++ monomaniac
mailto:matthew (AT) stlsoft (DOT) org
http://www.stlsoft.org
news://news.digitalmars.com/c++.stlsoft
"But if less is more, think how much more more will be!" -- Dr Frazier
Crane
-------------------------------------------------------------------------------
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David B. Held Guest
|
Posted: Tue Aug 26, 2003 7:08 pm Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
"Matthew Wilson" <stlsoft (AT) hotmail (DOT) com> wrote
| Quote: | [...]
AFAIUI EBO is not what's going on here. Maybe my example
gave the wrong impression. I'll rephrase
[...]
EBO would only apply if the base classes Simple and Poly
were empty, which they're not. It's the derived class
empty_derived that is empty hence E*D*O.
|
Yes, the fact that your original example had all empty classes
made it impossible to know that this was the precise distinction
you were making. Anyway, after looking at 9.3, 10.5 and a
few other places, I suspect that this optimization is allowed, but
not explicitly mentioned or implied in the standard. That's
because the standard doesn't say how big a derived class has
to be other than that it has to have non-zero size (like all class
types). Which means that anything which observes all the other
rules for layout/size/storage are legal, and I'm not aware of any
rules which constrain the derived class in this case to be larger.
I would say that because the base subobject and derived
complete object can share the same address, that EDO must
be legal (except possibly in the case of MI).
Dave
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daveed Vandevoorde Guest
|
Posted: Sat Aug 30, 2003 7:36 pm Post subject: Re: Empty Derived Optimisation (EDO) ??? |
|
|
[email]stlsoft (AT) hotmail (DOT) com[/email] (Matthew Wilson) wrote:
[...]
| Quote: | So my question is, is this behaviour a nice feature of the
implementations (and not a hard one to achieve, one would think), or
is it a recognised part of the language that all compilers *should*
provide?
|
The former.
(Note that the base could be a POD, whereas the derived class
is certainly not.)
Daveed
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|