If we use a template-light-solution (as shown above) like the following:
namespace std {
template<typename T>
std::string to_string(const T &n) {
std::ostringstream s;
s << n;
return s.str();
}
}
Unfortunately, we will have problems in some cases. For example, for static const members:
hpp
class A
{
public:
static const std::size_t x = 10;
A();
};
cpp
A::A()
{
std::cout << std::to_string(x);
}
And linking:
CMakeFiles/untitled2.dir/a.cpp.o:a.cpp:(.rdata$.refptr._ZN1A1xE[.refptr._ZN1A1xE]+0x0): undefined reference to `A::x'
collect2: error: ld returned 1 exit status
Here is one way to solve the problem (add to the type size_t):
namespace std {
std::string to_string(size_t n) {
std::ostringstream s;
s << n;
return s.str();
}
}
HTH.