Why is there no std::stou?

后端 未结 3 1237
时光取名叫无心
时光取名叫无心 2020-12-10 00:31

C++11 added some new string conversion functions:

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

It includes stoi (string to int), stol (string t

3条回答
  •  时光说笑
    2020-12-10 00:56

    I've no idea why stoi exists but not stou, but the only difference between stoul and a hypothetical stou would be a check that the result is in the range of unsigned:

    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::max()) {
            throw std::out_of_range("stou");
        }
        return result;
    }
    

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

提交回复
热议问题