I have done explicit specializations before, I just can't figure out why this does not work:
StringUtils.hpp
#ifndef AYC_STRINGUTILS_HPP
#define AYC_STRINGUTILS_HPP
#include <string>
class StringUtils {
public:
template <typename T>
static std::string toString(const T& t);
};
#include "StringUtils.tpp"
#endif //AYC_STRINGUTILS_HPP
StringUtils.tpp
#include "StringUtils.hpp"
template<typename T>
std::string StringUtils::toString(const T& t) {
return std::to_string(t);
}
template<>
std::string StringUtils::toString<std::string>(const std::string& t) {
return t;
}
The errors I get are linker errors complaining about multiple definitions of the function toString.
Many files in the project use #include "StringUtils.hpp".
How can I try to fix this error? Is something wrong in the class StringUtils?
In addition to the solution provided in the answer by Brian, you can declare the specialization in the .hpp/.tpp file and define it in a .cpp file.
StringUtils.hpp file:
StringUtils.cpp file:
Test program:
Output of the test program: