0

I have a static template function in my class like that:

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;
}

file.cpp:

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

readImpl just return a string.

So, my question is why my specialization of the read method to std::string is not called when I run something like that:

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

Thanks

Sassa
  • 1,673
  • 2
  • 16
  • 30
  • 1
    [Templates and template specialization should be in headers](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Revolver_Ocelot Dec 14 '15 at 12:11

1 Answers1

0

You can make following thing:

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);

But actually it will be better to write specialization in header file.

ForEveR
  • 55,233
  • 2
  • 119
  • 133