我想知道如何正确循环一个普通的旧数据类型的成员,以获得他们的一些类型信息。那就是:循环POD成员
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++中,我们不能做简单的类型内省而不做一些棘手的戏剧与模板。但在这里,我无法找到具体的解决方案,即使事实上使用了宏。问题更多的是关于我而不是解决方案的存在。 :-)
我不一定要求一个不会侵入的解决方案。
在此先感谢。
这是不可能做 –
你有没有考虑过使用['std :: tuple'](http://en.cppreference.com/w/cpp/utility/tuple)而不是POD类型。 –
@CaptainObvlious是否std :: tuple是POD,如果与POD模板参数一起使用? – Nemikolh