测试2个类型相同的方法

类别:VC语言 点击:0 评论:0 推荐:

    也许有的时候,我们需要测试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;
                   }
        };
     嗬嗬,当然了,肯定也有别的方法,提出来一起分享:)

本文地址:http://com.8s8s.com/it/it1646.htm