Question

struct A {
   // ... some methods ...
   std::vector<int> foo;
   // ... more data members ...
};

With g++4.7 and libstdc++ I get std::is_standard_layout<A>::value == true.
But what happens with other compilers or standard-libraries?
Are there any guarantees that (at least certain?) STL-containers will not break the standard-layout?

Background:

struct B : A { // still standard-layout
   // ... more methods (but no new variables!)
   void bar();
};

This allows the use of static_cast<B &>(a).bar() even for A a;. (I'm not saying this is good design!).

Was it helpful?

Solution

No, there are no guarantees.

The C++11 Standard explicitly mentions when a class must have standard layout (e.g. the mutex class, the atomic_flag class, etc.).

The word "layout" does not appear in the whole Clause 23 (Containers Library). I believe this is sufficient to assume that no guarantees are given.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top