Search code examples
c++c++11tostringstringstreamstream-operators

Why Isn't to_string Templatized?


I thought that to_string was just templatized and used stringstream under the hood.

Is that not the case?

I want to be able to do this:

class foo{};

ostream& operator<<(ostream& os, const foo& /*bar*/){
    os << "foo";
    return os;
}

int main() {
    foo bar;
    string tsTest = to_string(bar);

    return 0;
}

But obviously that doesn't work cause to_string isn't templatized.


Solution

  • No, to_string is not for any type. There are only overloads for primitive standard types. It's not replace of boost::lexical_cast unfortunately.