Stoi and Std::To_String on Mingw 4.7.1

to_string is not a member of std, says g++ (mingw)

This is a known bug under MinGW. Relevant Bugzilla. In the comments section you can get a patch to make it work with MinGW.

This issue has been fixed in MinGW-w64 distros higher than GCC 4.8.0 provided by the MinGW-w64 project. Despite the name, the project provides toolchains for 32-bit along with 64-bit. The Nuwen MinGW distro also solves this issue.

MinGW 4.8.1 can't use C++11

I tested this in my MinGW 4.8.1 installation and, indeed, stoi() is not recognized, for whatever reason. However, its functionality can be easily replaced using strtol(), which does work fine in 4.8.1. Of course, the parameter types are somewhat different between the two functions, but the differences can be accounted for trivially. Please see documentation for the two functions.

stoi and stoll in c++

These functions are new in C++11, and GCC only makes it available if you specify that version of the language using the command-line option -std=c++11 (or -std=c++0x on some older versions; I think you'll need that for version 4.5).

If you can't use C++11 for some reason, you could convert using string streams:

#include <sstream>

template <typename T> from_string(std::string const & s) {
std::stringstream ss(s);
T result;
ss >> result; // TODO handle errors
return result;
}

or, if you're feeling masochistic, the C functions in such as strtoll declared in <cstring>.



Related Topics



Leave a reply



Submit