Issue
I have two structures in c++
struct Vertex {
float x,y,z;
float nx, ny, nz;
};
struct SkinnedVertex : public Vertex {
uint32_t j0, j1, j2, j3;
float w0, w1, w2, w3;
}
Now, when I use offsetof
to get offset of Vertex
, everything is fine but if I use offsetof
in SkinnedVertex
, I get the following warning when compiling with GCC:
warning: offsetof within non-standard-layout type ‘SkinnedVertex’ is conditionally-supported
My guess is that this happens because I am inheriting SkinnedVertex
from Vertex
, which makes this a non-standard-layout. But why does this happen? Does inheritance add additional information that makes this a non standard layout? Is thee a way to enforce it so that both of these structures are in standard layout?
EDIT: Changed jN types to uint32_t (was float)
Solution
https://en.cppreference.com/w/cpp/named_req/StandardLayoutType
A standard layout type requires all data members be in the same type.
Standard layout is conservative in definition. Additional types could be made standard layout. The fact that a type is not standard layout doesn't mean there is a good reason that it isn't. It just means the standard committee and compilers haven't done the work to justify it. I mean, you could make classes with virtual functions standard layout if the committee really wanted (like, standardize how vtables work).
One concern is that the committee doesn't want to needlessly break existing code. So they try to standardize existing practice unless there is a good reason. If two compilers disagree on layout of a class, that makes standardizing their layout more costly and/or generate less benefits.
Standard layout is technically just a flag; semantically it means different compilers with similar options (like packing) are going to compile compatible data layouts.
Answered By - Yakk - Adam Nevraumont Answer Checked By - David Goodson (WPSolving Volunteer)