相关疑难解决方法(0)

C++模板构造函数的特化

我有一个模板化的类A <T,int>和两个typedef A <string,20>和A <string,30>.如何覆盖A <string,20>的构造函数?以下不起作用:

template <typename T, int M> class A;
typedef  A<std::string, 20> one_type;
typedef  A<std::string, 30> second_type;


template <typename T, int M>
class A {
public:
  A(int m) {test= (m>M);}

  bool test;

};


template<>
one_type::one_type() { cerr << "One type" << endl;}
Run Code Online (Sandbox Code Playgroud)

我希望类A <std :: string,20>做一些其他类没有的东西.如何在不更改构造函数的情况下执行此操作A:A(int)?

c++ templates constructor specialization

8
推荐指数
4
解决办法
2万
查看次数

模板类参数类型的模板类成员的专门化

我有一个模板类Matrix.我想专门为类型复杂的函数,其中T可以是任何东西.我试过这个:

  6 template <typename T>
  7 class Matrix {
  8       public :
  9             static void f();
 10 };          
 11 template<typename T> void Matrix<T>::f() { cout << "generic" << endl; }
 12 template<> void Matrix<double>::f() { cout << "double" << endl; }
 13 template<typename T> void Matrix<std::complex<T> >::f() { cout << "complex" << endl; }
Run Code Online (Sandbox Code Playgroud)

第13行无法编译.我怎样才能做到这一点 ?

c++ templates function member

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

标签 统计

c++ ×2

templates ×2

constructor ×1

function ×1

member ×1

specialization ×1