Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #14956
| From | edA-qa mort-ora-y <mortoray@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Why is trivially copyable not standard-layout? |
| Date | 2012-03-17 10:54 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <13449926.187.1332006844968.JavaMail.geo-discussion-forums@vbat19> (permalink) |
I am wondering why C++11 decided to have both trivially copyable and standard layout types as opposed to just having standard layout. My question is motivated by the below code, which works (at least in gcc) but is not standards compliant. I was hoping the standard would clarify such use and make it legal, but alas it has not.
struct base { /* is a trivial class*/ };
struct derived : public base { /*still a trivial class*/ }
void copy( base * a, base * b, size_t len )
{
memcpy( a, b, len );
}
...
derived d1, d2;
copy( &d1, &d2, sizeof(derived) );
The standard ends up saying that though you can memcpy the type to a character buffer, that resulting buffer is 100% opaque and can *only* be used to copy back to the exact same type. The approach shown above, where you would copy via a base class pointer is not supported.
I'm just looking for an understanding as to this decision. Perhaps somebody can indicate a compiler (non-historic) that would actually fail (not properly copy) with the above code.
Back to comp.lang.c++ | Previous | Next | Find similar | Unroll thread
Why is trivially copyable not standard-layout? edA-qa mort-ora-y <mortoray@gmail.com> - 2012-03-17 10:54 -0700
csiph-web