Field offsetof
Closed this issue · 4 comments
Apparently getting the offset of a field within a struct from a pointer-to-member is not trivial (see).
It would be very helpful to get a field's offset from its respective field_descriptor
.
That would only work on standard-layout types, no?
I think it is a good idea to provide it somehow, just clarifying the restrictions the standard imposes. Non-std::is_standard_layout
types would not have field offsets.
Yes, you are right. offsetof
is only defined for standard layout types. I forgot about this restriction.
Would that restriction prevent you from using it? It might be nice to know what the use-case is :)
Wouldn't restrict me at all. My use-case is the same as in the reference from my initial post.
Given the definition of a vertex:
struct StandardVertex {
vec3 position;
vec3 normal;
vec4 tangent;
vec2 uv;
};
I'd like to generate the shader input description, required by the graphics API, using reflection. Currently I call offsetof
manually and store that value in an attribute:
REFL_TYPE(ikaros::StandardVertex)
REFL_FIELD(position, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, position), "POSITION"));
REFL_FIELD(normal, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, normal), "NORMAL"));
REFL_FIELD(tangent, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, tangent), "TANGENT"));
REFL_FIELD(uv, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, uv), "TEXCOORD", 0));
REFL_END
However, this feature is not critical at all. I am not 100% convinced yet myself whether this is the best approach. But I feel like having the field offset being part of the descriptor would be very helpful for this case, and probably a few others. The standard layout restriction isn't too problematic, one can practically always wrap a management class around a standard layout struct.