c++ - POD implications for a struct which holds an standard library container -
i came across this question recently. goal understand how c++ compiler views struct definitions hold standard library containers such std::vector.
ben voigt's answer linked question cites following c++0x standard:
....
a trivial class class has trivial default constructor (12.1) , trivially copyable.
[ note: in particular, trivially copyable or trivial class not have virtual functions or virtual base classes. — end note ]
a standard-layout class class that:
- has no non-static data members of type non-standard-layout class (or array of such types) or reference,
....
i'm bolded text implies following undefined behavior
struct { std::vector< sometype > myvec; int myc; a( int c ) : myc : (c) {} }; int main( void ) { one( 1 ); two( 2 ); sometype k, z; one.myvec.push_back( k ); two.myvec.push_back( z ); memcpy( &two, &one, sizeof( ) ); // bad juju }
and same case any type standard library, including simpler types such std::string
. due nature of library's design, given large usage of inheritance , template programming.
so, while struct a
resemble of pod type, fact contains standard library type automatically invalidates category, far compiler concerned.
are assumptions correct?
no. basic assumptions flawed. "standard layout" not related templates. e.g. std::pair<t1, t2>
has standard layout if , if both t1
, t2
do. same goes std::array<t,n>
however, none of containers have standard layout. whole point of allocators have advanced memory management.
Comments
Post a Comment