17

C++11 introduces convenience functions stoi, stol, stoll, stoul, stoull, stof, stod, and stold, which convert a string to an integer, long, long long, unsigned long, unsigned long long, float, double, or long double, respectively.

Why no love for short and unsigned short?

Besides the fact that the omission annoys me out of principle, I am finding myself having to work awkwardly around situations like this:

#include <string>

struct S
{
    S(short);
};

int main()
{
    S s{std::stoi("4")};
}

Error:

test.cpp: In function 'int main()':
test.cpp:10:23: error: narrowing conversion from 'int' to 'short int' inside { } [-fpermissive]

I'd like to instead write S s{std::stos("4")};, if only there were an stos...

Instead I have to write S s{static_cast<short>(std::stoi("4"))};... oh wait, that won't do it either, that will silently truncate integers longer than shorts, as opposed to a hypothetical stos function which would throw an exception if the integer does not fit into a short. So basically I'm back to my pre-C++11 alternatives of stringstreams, boost::lexical_cast, and the like.

EDIT: Since people seem to have a hard time locating my actual question, it's why are there no stos and stous functions to go along with the other ones?

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • And what is the Q? If you know these functions don't exist, they can't be added to the Standard now, You will need to wait a few decades till the next standard arrives.Simple Answer: Standards committee wouldn't have found any compelling reasons to find it as useful as other functions, they couldn't include anything and everything which seemed useful, So they chose what *they* felt would be most useful. – Alok Save Oct 25 '11 at 20:43
  • 1
    @Adam: Sure, but I could have written my own `stoi`, and `stol`, too. The whole point of standardizing these things is that people don't have to write them over and over again. – HighCommander4 Oct 25 '11 at 20:46
  • Why doesn't C++ have a function to frob widgets? – Adam Rosenfield Oct 25 '11 at 20:47
  • 3
    @AdamRosenfield: So that We could blame the Standards committee for not adding it! ;-) – Alok Save Oct 25 '11 at 20:48
  • 1
    @Als: I am curious to know why conversion functions for 8 of the 10 built-in integer types are deemed useful, but not the two. That's what I'm asking in this question. – HighCommander4 Oct 25 '11 at 20:53
  • @Mark: Good point, they should have that too. The choice seems so random... – HighCommander4 Oct 25 '11 at 21:03
  • What about `std::stouli`, `std::stoli`, `std::stolf`, my goodness they were slacking. – AJG85 Oct 25 '11 at 21:13
  • 3
    @AJG85: Umm, "long int" and "unsigned long int" are synonms for "long" and "unsigned long", and there is no such thing as a "long float". – HighCommander4 Oct 25 '11 at 21:15
  • 2022 and nobody discusses at least a workaround or a best practice. Instead let's close the question. – Vinícius Ferrão Feb 20 '22 at 03:33

1 Answers1

4

A guess: C++ took the s-to-xxx functions from C (probably the C99 variant) just for compatibility; there would be no such functions if C++ were developed independently.

anatolyg
  • 26,506
  • 9
  • 60
  • 134