【问题标题】:Traits class implementation for generic types with different type params具有不同类型参数的泛型类型的特征类实现
【发布时间】:2013-02-05 11:47:03
【问题描述】:

我有以下特质类

template<typename T> struct FeatureType;

我就是这样使用它的,效果很好:

class Foo { };
template<> struct FeatureType<Foo> {
  typedef int value;
};

template<typename T> class Bar { };
template<typename T> struct FeatureType<Bar<T>> {
  typedef T value;
};

有没有办法将泛型类型的这种实现扩展到具有多个类型参数的那些(不像上面的Bar)?以下不起作用

template<typename A, typename B> class Huh { };
template<typename A, typename B> struct FeatureType<Huh<A,B>> {
  typedef A value;
};

谢谢!

【问题讨论】:

    标签: c++ templates c++11 variadic-templates typetraits


    【解决方案1】:

    常规模板

    常规模板不会对其模板参数进行重载,但您可以对任意多个模板参数进行部分特化。只要您将; 放在每个结构声明/定义后面,您的代码就应该可以工作。 (请注意,将模板内的嵌套类型表示为type,将值表示为value 是一种习惯):

    #include <iostream>
    
    template<typename T>
    struct FeatureType;
    
    class Foo { };
    template<> struct FeatureType<Foo> 
    {
      typedef int type;
      type value;
    };
    
    template<typename T> class Bar { };
    template<typename T> struct FeatureType<Bar<T>> 
    {
      typedef T type;
      type value;
    };
    
    template<typename A, typename B> class Huh {};
    template<typename A, typename B>
    struct FeatureType< Huh<A,B> > 
    { 
       typedef A type; 
       type value;
    };
    
    int main()
    {
      FeatureType<Foo> f0;
      f0.value = 0;
    
      FeatureType< Bar<int> > f1;
      f1.value = 1;
    
      FeatureType< Huh<int, int> > f2;
      f2.value = 2;
    
      std::cout << f0.value << f1.value << f2.value;   
    }
    

    LiveWorkSpace 上的输出(gcc 4.7.2)

    注意:即使您有多个形式模板参数(AB 或任意数量),实际模板也是部分专用对于单个班级Huh&lt;A, B&gt;

    可变参数模板

    如果你真的想让FeatureType 的多个版本采用不同数量的模板参数,你需要使用可变参数模板(C++11)

    #include <iostream>
    
    template<typename... Args>
    struct FeatureType;
    
    template<> struct FeatureType<int> 
    {
      typedef int type;
      type value;
    };
    
    template<typename T> struct FeatureType< T > 
    {
      typedef T type;
      type value;
    };
    
    template<typename A, typename B>
    struct FeatureType< A, B > 
    { 
       typedef A type; 
       type value;
    };
    
    int main()
    {
      FeatureType< int > f0;
      f0.value = 0;
    
      FeatureType< int > f1;
      f1.value = 1;
    
      FeatureType< int, int > f2;
      f2.value = 2;
    
      std::cout << f0.value << f1.value << f2.value;   
    }
    

    LiveWorkSpace 上的输出

    【讨论】:

      【解决方案2】:

      我不确定你到底尝试了什么,但你肯定可以专攻任意数量的模板参数:

      template <typename A, typename B>
      class foo { };
      
      template <typename T>
      struct feature_type {};
      
      template <typename A, typename B>
      struct feature_type<foo<A,B>> {
        typedef A type1;
        typedef A type2;
      };
      
      int main(int argc, const char* argv[])
      {
        typename feature_type<foo<int,char>>::type1 x;
        typename feature_type<foo<int,char>>::type2 y;
        return 0;
      }
      

      See it in action.

      【讨论】:

        猜你喜欢
        • 2021-07-02
        • 1970-01-01
        • 1970-01-01
        • 2013-11-22
        • 2018-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        相关资源
        最近更新 更多