我想知道如何正確循環一個普通的舊數據類型的成員,以獲得他們的一些類型信息。那就是:循環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