Why is trivially copyable not standard-layout?

Discussion in 'C++' started by edA-qa mort-ora-y, Mar 17, 2012.

  1. 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 itlegal, 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 wouldcopy via a base class pointer is not supported.

    I'm just looking for an understanding as to this decision. Perhaps somebodycan indicate a compiler (non-historic) that would actually fail (not properly copy) with the above code.
    edA-qa mort-ora-y, Mar 17, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    572
    John Timney \(MVP\)
    Jun 19, 2006
  2. Alex Martelli

    PEP 323: Copyable Iterators

    Alex Martelli, Oct 29, 2003, in forum: Python
    Replies:
    2
    Views:
    303
    John Roth
    Oct 29, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,816
    Smokey Grindel
    Dec 2, 2006
  5. Juha Nieminen
    Replies:
    2
    Views:
    334
    James Kanze
    Sep 5, 2008
Loading...

Share This Page