Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用模板化类进行专门化_C++_Templates_Template Specialization_Specialization - Fatal编程技术网

C++ C++;使用模板化类进行专门化

C++ C++;使用模板化类进行专门化,c++,templates,template-specialization,specialization,C++,Templates,Template Specialization,Specialization,我无法用模板类专门化模板类函数 class Incrementer { int calculate(int n) {return n++; } }; template <int N> class Multiplier { int calculate(int n) { return n * N; } }; template <class C> class Storer { void Store(const C&, std::ostream&

我无法用模板类专门化模板类函数

class Incrementer
{
  int calculate(int n)  {return n++; }
};

template <int N>
class Multiplier
{
  int calculate(int n) { return n * N; }
};

template <class C>
class Storer
{
  void Store(const C&, std::ostream&) const;
  void ToFile(const C& toStore, const std::string& fileName) const { Store(toStore, std::ofstream(fileName)); }
};

template <> void Storer<Incrementer>::Store(const Incrementer& incrementer, std::ostream& os) const
{
  os << "Incrementer";
}

template <int N> template<> void Storer<Multiplier<N>>::Store(const Multiplier<N>& multiplier, std::ostream& os) const
{
  os << "Multiplier." << N;
}
类递增器
{
int计算(int n){返回n++;}
};
模板
类乘数
{
int计算(int n){返回n*n;}
};
模板
类存储器
{
无效存储(常数C&,标准::ostream&)常数;
void ToFile(const C&toStore,const std::string&fileName)const{Store(toStore,std::ofstream(fileName));}
};
模板void Storer::Store(const Incrementer&Incrementer,std::ostream&os)const
{

os您只能专门化模板类本身,而不能专门化该类中的单个方法。但它对类递增器有效。允许完全专门化,但不允许部分专门化。如果删除类存储器,而存储只是一个模板函数:`template void Store(const C&,std::ostream&)如何使用模板化的类乘法器专门化存储?函数模板不能部分专门化,但可以重载函数。