The code in the link below doesn't work as intended. I don't know what I'm doing wrong. https://coliru.stacked-crooked.com/a/f6ac59c7c20be10c
The code can be seen below, and the error message is in the link above.
#include <iostream>
#include <string>
namespace Logger
{
struct IStringable
{
virtual ~IStringable() {}
virtual std::string ToString() const = 0;
};
std::string to_string(IStringable const& v) { return v.ToString(); }
std::string to_string(const char* const& v) { return std::string(v); }
template<class T>
void log(T const& v)
{
using std::to_string;
std::cout << "debug: " << to_string(v) << '\n';
}
}
class Person : public Logger::IStringable {
public:
Person(const std::string name) : _name(name) { }
virtual std::string ToString() const { return _name; }
private:
std::string _name;
};
int main()
{
Person p("Alice");
double d = 0.0;
const char* c = "Some words";
Logger::log(p); // Works
Logger::log(d); // Works
Logger::log(c); // Error
}
g++ -std=c++17 -O2 -Wall -Wextra -Werror -pedantic main.cpp && ./a.out
main.cpp: In instantiation of 'void Logger::log(const T&) [with T = const char*]':
main.cpp:39:18: required from here
main.cpp:19:44: error: no matching function for call to 'to_string(const char* const&)'
std::cout << "debug: " << to_string(v) << '\n';
~~~~~~~~~^~~