模板类的静态模板成员函数 [英] Static template member function for template class

查看:919
本文介绍了模板类的静态模板成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a template class and a template member function:

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

我想专门研究T1T2相同的情况,

I want to specialize for a case when T1 and T2 are the same,

例如,为任何T定义大小写A<T>::f<T>.

For example, define the case A<T>::f<T> for any T.

但是我找不到关键字的组合来实现这一目标.

but I can't find the combination of keywords to achieve this.

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

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

These are my unsuccessful attempts, and the error messages:

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

2) Specialize outside the class "simultaneously": fatal error: cannot specialize a member of an unspecialized template

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

3)专门使用template<>: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<>') 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

5) Specialize the whole class (based on the error of attempt 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>

6) Specialize the class but not the function (?): 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生成错误消息.

推荐答案

您不能部分专门化一个函数.

You cannot partially specialize a function.

您可以将作品转发到另一种类型:

You can forward the work to another type:

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;
  }
  tempalte<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的类型进行调度.

where we directly dispatch on the type of T2.

这篇关于模板类的静态模板成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆