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++ 未调用std::string的专用静态模板函数_C++_Templates_Static_Specialization - Fatal编程技术网

C++ 未调用std::string的专用静态模板函数

C++ 未调用std::string的专用静态模板函数,c++,templates,static,specialization,C++,Templates,Static,Specialization,我的类中有一个静态模板函数,如下所示: const std::string content = File::read<std::string>(path); file.hpp: class File { public: template<typename Type> static Type read(const std::string & fullPath, const std::ios_base::openmode mode = std::i

我的类中有一个静态模板函数,如下所示:

 const std::string content = File::read<std::string>(path);
file.hpp:

class File
{
public:
    template<typename Type>
    static Type read(const std::string & fullPath, const std::ios_base::openmode mode = std::ios_base::in);
};

template<typename Type>
Type File::read(const std::string & fullPath, std::ios_base::openmode mode)
{
  std::stringstream ss;
  ss << readImpl(fullPath, mode);

  Type value = Type();
  ss >> value;

  return value;
}
类文件
{
公众:
模板
静态类型读取(const std::string&fullPath,const std::ios\u base::openmode=std::ios\u base::in);
};
模板
类型文件::读取(常量std::字符串和完整路径,std::ios\u base::openmode)
{
std::stringstream-ss;
ss>值;
返回值;
}
file.cpp:

template<>
std::string File::read(const std::string & fullPath, std::ios_base::openmode mode)
{
  return readImpl(fullPath, mode);
}
模板
std::string File::read(常量std::string&fullPath,std::ios\u base::openmode)
{
返回readImpl(完整路径,模式);
}
readImpl只返回一个字符串

所以,我的问题是,当我运行类似于以下内容时,为什么没有调用我对std::string的read方法的专门化:

 const std::string content = File::read<std::string>(path);
const std::string content=File::read(路径);

谢谢

您可以做以下事情:

file.cpp

template<>
std::string File::read(const std::string & fullPath, std::ios_base::openmode mode)
{
  return readImpl(fullPath, mode);
}

template std::string File::read(const std::string&, std::ios_base::openmode);
模板
std::string File::read(常量std::string&fullPath,std::ios\u base::openmode)
{
返回readImpl(完整路径,模式);
}
模板std::string File::read(常量std::string&,std::ios\u base::openmode);
但实际上,最好在头文件中编写专门化