Re: empty base optimization of a multiple common base
On May 23, 9:43 pm, "Bo Persson" <b...@gmb.dk> wrote:
Nikolay Ivchenkov wrote:
On 20 May, 02:27, "Bo Persson" <b...@gmb.dk> wrote:
Nikolay Ivchenkov wrote:
On 19 May, 01:51, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On 17/05/10 19:26, itaj sherman wrote:
The standard requires that base class subobjects of the same type
have distinct addresses.
Where exactly the definitive text of the current standard contains
such a requirement?
There is a note at the end of section 10 "Derived classes"
referring
to pointer comparisons in section 5.10
This is not definitive part of the standard. See ISO/IEC Directives,
Part 2 (Rules for the structure and drafting of International
Standards) - 6.5.1:
"Notes and examples integrated in the text of a document shall only
be used for giving additional information intended to assist the
understanding or use of the document. These elements shall not
contain requirements or any information considered indispensable
for the use of the document."
I know, but section 5.10 is normative text. The note just points us
there.
In addition, objects can share a common region of storage as shown
below:
#include <iostream>
#include <string>
struct X
{
unsigned char buf[sizeof(std::string)];
};
int main()
{
X *p = new X;
new(p) std::string("text");
std::cout << *(std::string *)(void *)p->buf << std::endl;
((std::string *)(void *)p)->~basic_string<char>();
delete p;
}
Here the object of type std::string has the same address as:
- the object of type X,
- the array, and
- the first element of the array.
Two elements of *different* types can have the same address, like a
POD struct and its first member.
Objects of the same type cannot. If two pointers of the same type
compares equal, they either point to the same object, or no objects at
all (like the position after the end of an array).
They can not point to two different empty base classes of the same
type, and still compare equal. Therefore the base classes must have
different addresses.
Bo Persson
From section 10:
5 [Note: A base class subobject might have a layout (3.7) different
from the layout of a most derived object of
the same type. A base class subobject might have a polymorphic
behavior (12.7) different from the polymorphic
behavior of a most derived object of the same type. A base class
subobject may be of zero size
(clause 9); however, two subobjects that have the same class type and
that belong to the same most derived
object must not be allocated at the same address (5.10). ]
That means that in my original example the two base subobjects of type
A must have different addresses, because they have the same mode
derived type D. Do compilers actually comply with this?
Nikolay's example is different. It's a subobject of base class and a
data member. They are of the same type, but not of the same most
derived object. So this section of the standard doesn't apply.
itaj
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]