我想知道如何正确循环普通旧数据类型的成员,以便获取有关它们的一些类型信息。那是 :
struct my_pod
{
int a;
double b;
};
template<typename POD>
void loopOverPOD()
{
for_each(POD, member) // The magic part
{
// member::type should be for my_pod int, then double
typename member::type i;
// member::size_of should be equal to sizeof(int) then sizeof(double)
// Trivial if we can have member::type information.
int size = member::size_of;
// member::offset_of should be equal to 0, then sizeof(int)
// Trivial if we can have member::size_of information.
int offset = member::offset_of;
}
}
据我所知,在 C++ 中,如果不对模板进行一些棘手的操作,我们就无法进行简单的类型自省。但是在这里,我找不到使用模板的具体解决方案,即使实际上使用宏也是如此。问题更多是关于我,而不是关于解决方案的存在。:-)
我不一定要求一个不会侵入的解决方案。
提前致谢。