33
votes

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 32. 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 std::stoul, etc. Does std::stoi and family not exist in MinGW for some reason? I thought gcc on MinGW (sh|w)ould behave the same as on Linux.

4
@KerrekSB yes: g++ -std=c++0x test.cppSeth Carnegie
MinGW isn't maintained by the same group as GCC itself, so it's not always up to date.Benjamin Lindley
@BenjaminLindley does that mean that when you do g++ -v on MinGW and you get a certain version, it doesn't mean anything as to what features it implements?Seth Carnegie
@Seth: Well, that tells you the version of the compiler. It doesn't tell you much about the standard library.Benjamin Lindley
I have contacted the MinGW-w64 developers about this. To me, this is just libstdc++ devs being lazy.rubenvb

4 Answers

46
votes

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: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.

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.

6
votes

This is fixed in MinGW-w64, a fork of the original MinGW project that actually is interested in fixing bugs like this. It was fixed as of g++ 4.9.2, and maybe earlier.


Note: for people coming here who have done a default install of CodeBlocks (which comes with the old, broken MinGW), and want to upgrade the compiler, see this answer.

You can use any build of MinGW-w64: I use the self-installer from mingw-builds.org, whereas that answer uses TDM-GCC-64. If you want both 64bit and 32bit compilation you need to install and add 2 new compilers: mingw-w64 64-bit, and mingw-w64 32-bit. It does NOT support using one installation of g++ with the -m32 or -m64 switch to toggle.

0
votes

I am using MinGW 4.9.3-1. This problem seems to be still there. As a workaround, I used the another way of getting integers from strings.

int rows, columns;
sscanf(argv[1], "%d", &rows);
sscanf(argv[2], "%d", &columns);
0
votes

Use Mingw-w64. I had this same issue and using Mingw-w64 worked for me.