Template friendly string to numeric in C++

C++String Conversion

C++ Problem Overview


In C++ standard library there are functions to convert from string to numeric types:

stoi
stol
stoll
stoul
stoull
stof
stod
stold

but I find it tedious to use them in template code. Why there are no template functions something like:

template<typename T>
T sto(...)

to convert strings to numeric types?

I don't see any technical reason to not have them, but maybe I'm missing something. They can be specialised to call the underlying named functions and use enable_if/concepts to disable non numeric types.

Are there any template friendly alternatives in the standard library to convert string to numeric types and the other way around in an efficient way?

C++ Solutions


Solution 1 - C++

> Why there are no template functions something like:

C++17 has such generic string to number function, but named differently. They went with std::from_chars, which is overloaded for all numeric types.

As you can see, the first overload is taking any integer type as an output parameter and will assign the value to it if possible.

It can be used like this:

template<typename Numeric>
void stuff(std::string_view s) {
    auto value = Numeric{};

    auto [ptr, error] = std::from_chars(s.data(), s.data() + s.size(), value);

    if (error != std::errc{}) {
        // error with the conversion
    } else {
        // conversion successful, do stuff with value
    }
}

As you can see, it can work in generic context.

Solution 2 - C++

It's not a template, and it doesn't work with locales but if that isn't a show stopper then C++17 already has what you want: std::from_chars

There are overloads for all of the integer and floating-point types and the interface is the same except for the last parameters which are different for the integer and floating-point types respectively (but if the default is fine then you don't need to change anything). Because this is not a locale-aware function it is also quite fast. It will beat any of the other string to value conversion function and generally it is by orders of magnitude.

There is a very good CPPCON video about <charconv> (the header from_chars lives in) by Stephan T. Lavavej that you can watch about its usage and performance here: https://www.youtube.com/watch?v=4P_kbF0EbZM

Solution 3 - C++

You wouldn't gain much because in an expression like

int x = sto("1");

There is no (easy) way to deduce the desired type for the template parameter. You would have to write

int x = sto<int>("1");

which to some extend defeats the purpose of providing a generic function. On the other hand, a

template<typename T>
void sto(std::string x,T& t);

would be of good use as you realized. In C++17 there is std::from_chars, which does more or less exactly that (it is no template but a set of overloads and it takes pointers to chars instead of a string, but thats only minor details).

PS There is no easy way to deduce the desired type in the above expression, but there is a way. I dont think the core of your question was exactly the signature you asked for, and I dont think the following is a good way to implement it, but I knew that there is a way to make the above int x = sto("1"); compile and I was curious to see it in action.

#include <iostream>
#include <string>

struct converter {
    const std::string& x;
    template <typename T> operator T() { return 0;}
};

template <> converter::operator int() { return stoi(x); }
template <> converter::operator double() { return stod(x); }
converter sto(const std::string& x) { return {x}; }

int main() {
    std::string s{"1.23"};
    int x = sto(s);
    double y = sto(s);
    std::cout << x << " " << y;
}

This works as intended, but it has severe downsides, maybe most importantly it allows to write auto x = sto(s);, ie it is easy to use wrong.

Solution 4 - C++

Solution compatible with all (even older C++ compilers like C++-98 ones) is to use boost::lexical_cast which is a template to convert between numeric and string types in both ways.

Example:

short myInt = boost::lexical_cast<short>(*argv);
std::string backToString = boost::lexical_cast<std::string>(myInt);

See: https://www.boost.org/doc/libs/1_42_0/libs/conversion/lexical_cast.htm

Solution 5 - C++

On older C++ versions, stringstream is your friend. If I understand correctly, then the following might be interesting to you. It is C++11.

https://wandbox.org/permlink/nUNiUwWWTr7a0NXM

#include <sstream>
#include <string>
#include <iostream>

template<typename T, typename String>
T sto(const String & str) {
    T val;
    std::stringstream ss(str);
    ss >> val;
    return val;
}

template<typename T, typename String>
void sto(const String & str, T & val) {
    std::stringstream ss(str);
    ss >> val;
}

int main() {   
    std::cout << sto<float>("1.1") << ", " << sto<int>(std::string{"2"});
    
    // An alternative version that infers the type 
    double d;
    sto("3.3", d);
    std::cout << ", " << d;
}

This method works in C++11 and is pretty general. In my experience, this method is robust, but not the most performant.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMircea IspasView Question on Stackoverflow
Solution 1 - C++Guillaume RacicotView Answer on Stackoverflow
Solution 2 - C++NathanOliverView Answer on Stackoverflow
Solution 3 - C++463035818_is_not_a_numberView Answer on Stackoverflow
Solution 4 - C++Łukasz ŚlusarczykView Answer on Stackoverflow
Solution 5 - C++bremen_mattView Answer on Stackoverflow