|
|
测试2个类型相同的方法 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
也许有的时候,我们需要测试2个类型是否相同,恩,有几种方法你可以参考以下:
1、利用typeid,也许这是最常见的方法了: template<class T, class U> struct same_type { public: operator bool() { return typeid(T())==typeid(U()); } };
2、利用模板特化: template <typename T, typename U> struct same_type { private: template<typename> struct In { enum { value = false }; };
template<> struct In<T> { enum { value = true }; };
public: enum { value = In<U>::value }; };
3、利用Loki库里面的TypeList: template <typename T, typename U> struct same_type { public: operator bool() { return Loki::IndexOf<Loki::TYPELIST_2(T,U),U>==0; } }; 嗬嗬,当然了,肯定也有别的方法,提出来一起分享:)
|
|
相关文章:相关软件: |
|