As Some programmer dude pointed out in the comment, you cannot add declarations into std
namespace in this case.
Like this post, a workaround is to write your to_string
in the same namespace as your custom class, and use using std::to_string
to bring std::to_string
in overload resolution when you want to use to_string
for generic type. The following is an example:
#include <string>
struct A {};
std::string to_string(A) {return "";}
namespace detail { // does not expose "using std::to_string"
using std::to_string;
template <typename T>
decltype(to_string(std::declval<T>()), void(0)) foo() {}
}
using detail::foo;
int main()
{
foo<int>(); // ok
foo<A>(); // ok
// to_string(0); // error, std::to_string is not exposed
}