struct A {
// ... some methods ...
std::vector<int> foo;
// ... more data members ...
};使用g++4.7和libstdc++,我得到了std::is_standard_layout<A>::value == true。
但是其他编译器或标准库会发生什么呢?
是否有任何保证(至少是肯定的?)STL-containers不会破坏标准布局?
背景:
struct B : A { // still standard-layout
// ... more methods (but no new variables!)
void bar();
};这使得即使在A a;中也可以使用static_cast<B &>(a).bar()。(我并不是说这是一个好的设计!)
发布于 2013-03-04 00:30:02
no ,没有任何保证。
C++11标准明确提到了类何时必须具有标准布局(例如,mutex类、atomic_flag类等)。
“布局”一词不会出现在整个第23条(容器库)中。我相信这足以假设没有任何保证。
https://stackoverflow.com/questions/15187536
复制相似问题