Why not std :: stou?

In C ++ 11, some new string conversion functions were added:

http://en.cppreference.com/w/cpp/string/basic_string/stoul

It includes stoi (string to int), stol (string to long), stoll (long long), stoul (string to unsigned long), stoull (string to unsigned long long). Notable in its absence is the function stou (string to unsigned). Is there any reason why this is not necessary, but everyone else?

related: Is there no "sto {short, unsigned short}" function in C ++ 11?

+68
c ++ string c ++ 11 std
Jan 03 '12 at 16:21
source share
2 answers

The most expensive answer will be that the C library does not have the corresponding " strtou ", and the C ++ 11 string functions are just thinly veiled wrappers around the C library functions: std::sto* function mirror strto* , and the std::to_string functions std::to_string use sprintf .




Edit: As KennyTM points out, both stoi and stol use stol as the base conversion function, but it still remains cryptic why, when there is a stoul that uses strtoul , there are no matching stou .

+20
Jan 03 2018-12-01T00:
source share

I don't know why stoi exists, but not stou , but the only difference between stoul and a hypothetical stou will be to verify that the result is in the unsigned range:

 unsigned stou(std::string const & str, size_t * idx = 0, int base = 10) { unsigned long result = std::stoul(str, idx, base); if (result > std::numeric_limits<unsigned>::max()) { throw std::out_of_range("stou"); } return result; } 

(Similarly, stoi also similar to stol , just with a different range check, but since it already exists, there is no need to worry about how to implement it.)

+19
Jan 03 '12 at 17:05
source share



All Articles