【问题标题】:Static template member function for template class模板类的静态模板成员函数
【发布时间】:2015-10-17 02:56:57
【问题描述】:

我有一个模板类和一个模板成员函数:

template<class T1>
struct A{
    template<class T2>
    static int f(){return 0;}
};

我想专攻T1T2相同的情况,

例如,为任何T 定义大小写A&lt;T&gt;::f&lt;T&gt;

但我找不到实现此目的的关键字组合。

如何部分(?)专门化模板类和模板静态函数的组合?


这些是我的不成功尝试,以及错误消息:

1) 专攻类内:fatal error: cannot specialize a function 'f' within class scope)

template<class T1>
struct A{
    template<class T2>
    static int f(){return 0;}

    template<>
    static int f<T1>(){return 1;}

};

2) 课外“同时”专精:fatal error: cannot specialize a member of an unspecialized template

template<class T>
void A<T>::f<T>(){return 1;}

3) 专门使用template&lt;&gt;: fatal error: too few template parameters in template redeclaration

template<> template<class T>
void A<T>::f<T>(){return 1;}

4) 倒序:fatal error: cannot specialize (with 'template&lt;&gt;') a member of an unspecialized template

template<class T> template<>
void A<T>::f<T>(){return 1;}

5) 特化整个类(基于尝试3的错误):fatal error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list

template<class T> 
struct A<T>{
    template<>
    static int f<T>(){return 1;}
};

6) 专门化类但不专门化函数 (?):fatal error: too few template parameters in template redeclaration template<> template<class T1>

template<> template<class T>
int A<T>::f(){return 0;}

我使用clang 3.5 C++14 生成错误消息。

【问题讨论】:

    标签: c++ templates c++14 template-specialization partial-specialization


    【解决方案1】:

    你不能部分特化一个函数。

    您可以将作品转发给其他类型:

    template<class T1>
    struct A;
    
    template<class T1, class T2>
    struct A_helper {
      static int f() {
        return A<T1>::f_simple<T2>();
      }
    };
    template<class T>
    struct A_helper<T,T> {
      static int f() {
        return A<T>::f_special();
      }
    };
    
    template<class T1>
    struct A{
      template<class T2>
      static int f(){return A_helper<T1,T2>::f()}
      template<class T2>
      static int f_simple(){return 0;}
      static int f_special(){return -1;}
    };
    

    或者,更简单地说,没有A_helper

    template<class T1>
    struct A{
      template<class T2>
      static int f(){return f2<T2>(std::is_same<T1,T2>{});}
      template<class T2>
      static int f2(std::true_type){
        static_assert(std::is_same<T1,T2>{}, "bob!");
        return -1;
      }
      template<class T2>
      static int f2(std::false_type){return -1;}
    };
    

    你可以使用标签调度。

    另一种方法:

    template<class T>struct tag{};
    
    template<class T1>
    struct A{
      template<class T2>
      static int f(){return f2(tag<T2>{});}
      template<class T2>
      static int f2(tag<T2>) {return 0;}
      static int f2(tag<T1>) {return -1;}
    };
    

    我们直接在 T2 的类型上调度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      相关资源
      最近更新 更多