12

Why is there no

template <typename T>
T std::from_string(const std::string& s);

in the C++ standard? (Seeing how there's an std::to_string() function, I mean.)

PS - If you have an idea for the reason this was not adopted/considered, just answer/comment and say what it is, rather than downvoting. I'm not actually making a proposal that this be included in the standard.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) and family. – NathanOliver Aug 26 '16 at 15:04
  • @NathanOliver: That's not the same thing. – einpoklum Aug 26 '16 at 15:05
  • 5
    "[Why is the `std::sto`… series not a template?](http://stackoverflow.com/questions/37920924/why-is-the-stdsto-series-not-a-template)" may be of interest. – Quentin Aug 26 '16 at 15:06
  • 2
    @einpoklum: "*That's not the same thing.*" ... why isn't it? – Nicol Bolas Aug 26 '16 at 15:07
  • 1
    I don't understand the downvoting. This question is well-posed and legitimate. – Bathsheba Aug 26 '16 at 15:07
  • 1
    @einpoklum It is not exactly the same thing but a generic from string would be way to complicated considering all of the different sources and types you could convert to. – NathanOliver Aug 26 '16 at 15:08
  • 1
    @NicolBolas: 1. A single templated function. 2. Not limitied to numeric types. – einpoklum Aug 26 '16 at 15:08
  • 3
    @Bathsheba: "*I don't understand the downvoting. This question is well-posed and legitimate.*" No, it's not. The answers will be *arguments*, not facts. – Nicol Bolas Aug 26 '16 at 15:09
  • 2
    @einpoklum: "*1. A single templated function. 2. Not limitied to numeric types.*" [The `to_string` series are not template functions. And they are limited to numeric types.](http://en.cppreference.com/w/cpp/string/basic_string/to_string) Note that you cannot add *overloads* to the `std` namespace. – Nicol Bolas Aug 26 '16 at 15:10
  • 4
    @NicolBolas: The answers should be explanations of considerations which the standard committee has made. – einpoklum Aug 26 '16 at 15:11
  • @NicolBolas einpoklum is *looking for* a template that's not limited. – Quentin Aug 26 '16 at 15:12
  • 1
    @einpoklum: Which would only be possible if they had *considered* a `from_string`. Do you know if any such proposal exists? Have you [done the research to find any such proposal](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/)? – Nicol Bolas Aug 26 '16 at 15:12
  • 1
    @Quentin: "*einpoklum is looking for a template that's not limited.*" He claims that there should be a "from_string" because there is a "to_string". But `to_string` is not the inverse of his suggested `from_string`. – Nicol Bolas Aug 26 '16 at 15:13
  • 1
    @NicolBolas: That's not quite searchable. I searched around some, and did not find such a discussion, so I figured that there was some "obvious" reason I was missing. I also remember searching for from_string here on SO before, and it occured to me now that an answer explaining why it doesn't exist could be helpful to people. – einpoklum Aug 26 '16 at 15:15
  • 1
    @NicolBolas: I can see your opinion, and I guess mine differs from yours. That's the beauty of the voting system. But I was concerned about the net -3 at the point of my first comment. – Bathsheba Aug 26 '16 at 15:19
  • 1
    @Bathsheba I agree with you, as I want to know the answer and have found this question. -3 sounds to me hilariously wrong. Fortunately, it is at least positive at this moment. – Yongwei Wu Apr 18 '18 at 10:27
  • @YongweiWu: My answer is hovering on the brink ;-) – Bathsheba Apr 18 '18 at 10:29

2 Answers2

3

As Nicol Bolas pointed out, to_string is never a template, but just a group of overloaded functions. Making such functions templates are not good, as doing them generically is probably not efficient, and can only behave like stringstreams. So from_string, similarly, should not be function templates, but rather simple functions. Since their argument is the same type (std::string), their names should not be the same. So stoi, stof, and the like are really what you wanted.

to_string and sto* also behave similarly regarding the locale: they all respect the current C locale. The C++ IOStreams, on the contrary, are not affected by the C locale. They can be overridden only if they are created after the C++ global locale is set by std::locale::global, or are manually imbue’d with a different locale.

Yongwei Wu
  • 5,292
  • 37
  • 49
1

std::to_string() has a much tighter mandate: it consists of a well-defined set of non-templatised overloads and each overload has a type that is trivially stringifiable. So it's easy to define exactly what the functions should do.

Your proposal of std::from_string() is far broader. It would have tricky corners such as error handling considerations, precision settings, etc. The already-available stream functions that do this kind of thing are probably sufficient.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    Why would this be more tricky than what you have with operator>> and istreams? – einpoklum Aug 26 '16 at 15:19
  • 3
    The istream stuff has all sorts of modifiers for adjusting precision and radix - that would be difficult to encapsulate in a single function. – Bathsheba Aug 26 '16 at 15:19
  • 1
    Your 1st paragraph is exactly what I thought when I saw this question, aside from too opinion-based/broad... so +1. My 2nd thought was that @einpoklum just wants [`boost::lexical_cast`](http://www.boost.org/doc/libs/1_61_0/doc/html/boost_lexical_cast/examples.html#boost_lexical_cast.examples.strings_to_numbers_conversion). And since the Committee frequently adapt things from Boost, there's a very good search term to use when assessing whether they have considered this, as was argued in the comments on the question. – underscore_d Aug 26 '16 at 16:23