3

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.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • How about simply defining a `to_string` overload in `foo`'s namespace, and then importing that namespace via `using`? What's the need for the ostream in that code snippet? – peppe Jan 23 '15 at 15:00
  • 2
    `std::stringstream` is not the most efficient way to convert so maybe that's why its not templatized, so each overload uses a more efficient function? – Galik Jan 23 '15 at 15:04
  • @peppe The need for `operator<<` in the code snipit was I thought to allow `to_string` to use `stringstream` to insert "foo". Wherein lies the question, why isn't `to_string` using `stringstream` under the hood? – Jonathan Mee Jan 23 '15 at 15:17
  • @Galik Sounds like you have the actual answer. Any links to support this? I know that `stringstream` isn't tremendously efficient, but it seems like converting `float`s and `double`s would require a lot of work without it? – Jonathan Mee Jan 23 '15 at 15:19
  • 2
    It's obvious from [documentation](http://en.cppreference.com/w/cpp/string/basic_string/to_string) that `std::to_string` is a set of bog-standard overloads, not a function template. – Lightness Races in Orbit Jan 23 '15 at 15:49
  • @Galik: One might still expect that to be achieved with template specializations. This factor does not at all explain why `std::to_string` is not a template. – Lightness Races in Orbit Jan 23 '15 at 15:50
  • 1
    For starters, `std::to_string` is defined in terms of `sprintf`, not `std::ostringstream`. Quite possibly, the intent is that it can be implemented this way. – James Kanze Jan 23 '15 at 16:11
  • @Galik Globally speaking: it certainly would have been possible for the standard to specify `std::to_string` as a template using `std::ostringstream`, and then specify explicit overloads for the built-in numeric types. IMHO, this would probably have been a better solution; the built-in numeric types have all sorts of formatting options that you may or may not want to set, where as user defined types typically don't. – James Kanze Jan 23 '15 at 16:14
  • You might find folly::to from Facebook's open source library, folly, to be of interest: https://github.com/facebook/folly/blob/master/folly/test/ConvTest.cpp – Mark Jan 24 '15 at 06:56
  • @Mark I confess as soon as I read Facebook I thought this was spam. Anyway looking at the code I' having a hard time deciphering how it works. Is there some documentation somewhere? I had no idea that Facebook did anything with C++! – Jonathan Mee Jan 24 '15 at 15:24
  • @JonathanMee - Unfortunately I think the source code is the documentation. From looking at Conv.h, it looks like `enable_if` is used to determine what types are being used and then each type of conversion has its own handling logic. In general the philosophy applied seems to be that any conversion that would fail will either not compile or in the alternative, generate a runtime exception. Most of Facebook's backend services are actually written in C++. – Mark Jan 24 '15 at 19:27
  • @JonathanMee - If I had to guess, I'd also wager that the documentation from the D language's `std.conv` module is probably pretty close to being accurate. As best as I know, Andrei was the original author of both: http://dlang.org/phobos/std_conv.html – Mark Jan 24 '15 at 19:29

1 Answers1

3

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.

ForEveR
  • 55,233
  • 2
  • 119
  • 133