c++c++98

C++98 alternative to std::stoul?


I'm having trouble here with this code:

unsigned long value = stoul ( s, NULL, 11 );

that gives me this error with c++ 98

error: 'stoul' was not declared in this scope

It works on C++11, but I need this on C++98.


Solution

  • You can use strtoul from cstdlib:

    unsigned long value = strtoul (s.c_str(), NULL, 11);
    

    Some differences:

    1. The second argument of std::stoul is a size_t * which will be set to the position of the first character after the converted number, while the second argument of strtoul is of type char ** and points to the first character after the converted number.
    2. If no conversion happens, std::stoul throw a invalid_argument exception while strtoul does not (you must check the value of the second argument). Typically, if you want to check for error:
    char *ptr;
    unsigned long value = strtoul (s.c_str(), &ptr, 11);
    if (s.c_str() == ptr) {
         // error
    }
    
    1. If the converted value is out of range for an unsigned long, std::stoul throws a out_of_range exception while strtoul return ULONG_MAX and set errno to ERANGE.

    Here is custom version of std::stoul that should behave like the standard one and summarizes the difference between std::stoul and strtoul:

    #include <string>
    #include <stdexcept>
    #include <cstdlib>
    #include <climits>
    #include <cerrno>
    
    unsigned long my_stoul(std::string const& str, size_t *idx = 0, int base = 10) {
        char *endp;
        unsigned long value = strtoul(str.c_str(), &endp, base);
        if (endp == str.c_str()) {
            throw std::invalid_argument("my_stoul");
        }
        if (value == ULONG_MAX && errno == ERANGE) {
            throw std::out_of_range("my_stoul");
        }
        if (idx) {
            *idx = endp - str.c_str();
        }
        return value;
    }