小编use*_*920的帖子

专门化模板派生类的函数模板

我本质上有一个std::integral_constant包含变量的模拟版本,我想专门为这些类派生一个函数模板Base<T>,如下所示:

template<class T> struct Base{
  typedef T type;
  T t;
};

template<class T> struct A : Base<T>{
  static constexpr T value = 1;
};
template<class T> struct B : Base<T>{
  static constexpr T value = 2;
};

struct Unrelated{};

// etc.

template<class T> void foo(T t){
  //I would like to specialize foo for A and B and have a version for other types
}


int main(){
  foo(A<float>());//do something special based on value fields of A and …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization c++11

5
推荐指数
1
解决办法
150
查看次数

标签 统计

c++ ×1

c++11 ×1

template-specialization ×1

templates ×1