這應(yīng)該是模板元編程里的概念吧,有時候我們確實(shí)需要獲取成員變量的類型,這里提供一個方法借助模板的偏特化達(dá)到目的:
- template< typename P>
- struct MemberType;
- template< typename F, typename T >
- struct MemberType< F T::* >
- {
- typedef F member_type;
- typedef T class_type;
- };
假設(shè)我們有個結(jié)構(gòu)定義如下:
- struct Call
- {
- int ID;
- std::string Key;
- };
通過 MemberType< decltype( &Call::ID ) >::member_type 就可以獲得是變量 ID 的類型(int)了。
注:如果你的編譯器不支持 decltype, 可以使用 boost 庫里的 BOOST_TYPEOF 代替。
轉(zhuǎn)自:
http://blog.csdn.net/jadedrip/article/details/5583300