02 May 2014

Stupid Type Conversions in C++98

If you’re working on a C++98 project, you have my condolences.

There are a number of type conversions that newer versions of C++ make super easy, but which are not included in C++98.

Here’s my reference for these:

String to…


You need the equivalent of stoi(). Here’s mine:

static int stoi( std::string & s ) {
    int i;
    std::istringstream(s) >> i;
    return i;


Incredibly, C++98 has atof(const char*). (You’ll just need to call .c_str() on your std::strings.)

Number (int or double) to String

You need the equivalent of std::to_string(). Here’s mine:

#include <sstream>

template<typename T>
std::string to_string(const T & value) {
    std::ostringstream oss;
    oss << value;
    return oss.str();


Programming Cpp