C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Empty POD as a base

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Thomas Thiriez
Guest





PostPosted: Wed Aug 27, 2003 9:38 am    Post subject: Empty POD as a base Reply with quote



Consider this struct:

struct B
{
};

Obviously, it's a POD.
Therefore, I am allowed to copy instances of it with:

void CopyB(B &b1, const B &b2)
{
memcpy(reinterpret_cast<unsigned char *>(&b1),
reinterpret_cast<const unsigned char *>(&b2),
sizeof(B));
}

Let's inherit from B:

struct D : B
{
char c;
};

On my implementation that performs the EBO, I have sizeof(D) == 1.
Now, the question is:
Am I allowed to use CopyB to copy the base subobject of D ?
Let's try:

void f()
{
D d1, d2;
d1.c = 'A';
d2.c = 'B';
CopyB(d1, d2);
cout < }

Obviously, there is something wrong.
I guess either a POD is not allowed to be empty,
or a base subobject can't be considered as a POD anymore.

--
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Wed Aug 27, 2003 3:48 pm    Post subject: Re: Empty POD as a base Reply with quote



In article <3f4bddca$0$6205$626a54ce (AT) news (DOT) free.fr>, Thomas Thiriez
<thomas.thiriez (AT) free (DOT) fr> writes
Quote:
Consider this struct:

struct B
{
};

Obviously, it's a POD.

And the solution is to make such objects non-PODs. I may be wrong but I
think that they serve no useful purpose in C while being useful in C++
(e.g. as types for exception objects)

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Thu Aug 28, 2003 10:33 am    Post subject: Re: Empty POD as a base Reply with quote



In article <3f4bddca$0$6205$626a54ce (AT) news (DOT) free.fr>, Thomas Thiriez wrote:
Quote:
Consider this struct:

struct B
{
};

Obviously, it's a POD.
Therefore, I am allowed to copy instances of it with:

void CopyB(B &b1, const B &b2)
{
memcpy(reinterpret_cast<unsigned char *>(&b1),
reinterpret_cast<const unsigned char *>(&b2),
sizeof(B));
}

Let's inherit from B:

struct D : B
{
char c;
};

On my implementation that performs the EBO, I have sizeof(D) == 1.
Now, the question is:
Am I allowed to use CopyB to copy the base subobject of D ?
snip


No, you are only allowed to copy *complete* POD objects this way (standard
section 3.9 paragraph 2). You just found a reason for the limitation -
this kind of copying conflicts with the empty base optimisation.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
johnchx
Guest





PostPosted: Thu Aug 28, 2003 2:48 pm    Post subject: Re: Empty POD as a base Reply with quote

Thomas Thiriez <thomas.thiriez (AT) free (DOT) fr> wrote

Quote:
Obviously, there is something wrong.
I guess either a POD is not allowed to be empty,
or a base subobject can't be considered as a POD anymore.

The resolution to DR 43 (included in TC1) says that you can't memcpy a
base class subobject.

http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#43

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Dennett
Guest





PostPosted: Thu Aug 28, 2003 3:04 pm    Post subject: Re: Empty POD as a base Reply with quote

Thomas Thiriez wrote:
Quote:
Consider this struct:

struct B
{
};

Obviously, it's a POD.
Therefore, I am allowed to copy instances of it with:

void CopyB(B &b1, const B &b2)
{
memcpy(reinterpret_cast<unsigned char *>(&b1),
reinterpret_cast<const unsigned char *>(&b2),
sizeof(B));
}

So long as it is a complete object, std::memcpy will work.

Quote:
Let's inherit from B:

struct D : B
{
char c;
};

On my implementation that performs the EBO, I have sizeof(D) == 1.
Now, the question is:
Am I allowed to use CopyB to copy the base subobject of D ?

No.

Quote:
Let's try:

void f()
{
D d1, d2;
d1.c = 'A';
d2.c = 'B';
CopyB(d1, d2);
cout < }

Obviously, there is something wrong.

The code :)

Quote:
I guess either a POD is not allowed to be empty,
or a base subobject can't be considered as a POD anymore.

Or (c), it's a POD, and it's empty, but you can't rely on
copying POD subobjects.

-- James.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dave Harris
Guest





PostPosted: Fri Aug 29, 2003 10:56 am    Post subject: Re: Empty POD as a base Reply with quote

[email]francis (AT) robinton (DOT) demon.co.uk[/email] (Francis Glassborow) wrote (abridged):
Quote:
struct B
{
};

Obviously, it's a POD.

And the solution is to make such objects non-PODs. I may be wrong
but I think that they serve no useful purpose in C while being
useful in C++ (e.g. as types for exception objects)

Possible uses in C include as a placeholder (eg, when members may be
added to B in the next version), and in conjunction with:

(B *) malloc( sizeof(B) );

as a way of getting a unique, statically typed identifier.

-- Dave Harris, Nottingham, UK

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.