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);
但实际上,最好在头文件中编写专门化