C ++模板元编程,成员变量数? [英] C++ template meta-programming, number of member variables?
本文介绍了C ++模板元编程,成员变量数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中是否可以确定泛型类中的变量/字段数? 例如
Is it possible in C++ to determine number of variables/fields in the generic class? for example
// suppose I need metaclass number_members determines number of members
struct example { int i, j; };
assert(number_members<example>::value==2);
我浏览了mpl,但找不到实现.
I looked through mpl but could not find implementation.
谢谢.
推荐答案
否. C ++不提供对结构的一般自省.
No. C++ does not provide general introspection into structures.
您可以尝试使用C ++ 0x std::tuple
,它具有常规POD struct
的某些功能.或者,尝试从增强MPL 库.如果您刚开始使用C ++,那会有点先进.
You can try a C++0x std::tuple
, which has some of the features of a general POD struct
. Or, try to roll your own from the Boost MPL library. That would be a bit advanced if you're just getting started with C++.
这篇关于C ++模板元编程,成员变量数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文