Seth Carnegie Seth Carnegie - 11 months ago 90
C++ Question

std::stoi doesn't exist in g++ 4.6.1 on MinGW

I tried compiling this simple program on IdeOne (which uses gcc 4.5.1) and on my Linux computer (which uses something like 4.6.4):

#include <string>
#include <iostream>

int main() {
std::cout << std::stoi("32") << std::endl;

And it compiles perfectly and outputs
. However, when I try to compile it on my windows computer with MinGW and gcc 4.6.1, I get this error:

test.cpp: In function 'int main()':
test.cpp:5:19: error: 'stoi' is not a member of 'std'

The same happens with
, etc. Does
and family not exist in MinGW for some reason? I thought gcc on MinGW (sh|w)ould behave the same as on Linux.

Answer Source

This is a result of a non-standard declaration of vswprintf on Windows. The GNU Standard Library defines _GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can read more about this issue and macro here:

If you're willing to modify the header files distributed with MinGW, you may be able to work around this by removing the !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of .../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding it back around lines 2905 to 2965 (the lines that reference std::vswprintf). You won't be able to use the std::to_wstring functions, but many of the other conversion functions should be available.