Why is there no std::from_string()?
Asked Answered
B

2

13

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.

Beaded answered 26/8, 2016 at 15:2 Comment(17)
std::stoi and family.Xi
@NathanOliver: That's not the same thing.Beaded
"Why is the std::sto… series not a template?" may be of interest.Alkyl
@einpoklum: "That's not the same thing." ... why isn't it?Goyette
I don't understand the downvoting. This question is well-posed and legitimate.Fakieh
@Beaded 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.Xi
@NicolBolas: 1. A single templated function. 2. Not limitied to numeric types.Beaded
@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.Goyette
@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. Note that you cannot add overloads to the std namespace.Goyette
@NicolBolas: The answers should be explanations of considerations which the standard committee has made.Beaded
@NicolBolas einpoklum is looking for a template that's not limited.Alkyl
@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?Goyette
@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.Goyette
@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.Beaded
@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.Fakieh
@Fakieh 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.Dross
@YongweiWu: My answer is hovering on the brink ;-)Fakieh
D
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.

Dross answered 18/4, 2018 at 11:4 Comment(0)
F
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.

Fakieh answered 26/8, 2016 at 15:5 Comment(3)
Why would this be more tricky than what you have with operator>> and istreams?Beaded
The istream stuff has all sorts of modifiers for adjusting precision and radix - that would be difficult to encapsulate in a single function.Fakieh
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 @Beaded just wants boost::lexical_cast. 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.Beals

© 2022 - 2024 — McMap. All rights reserved.