Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

gcc

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 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.

like image 689
Seth Carnegie Avatar asked Dec 17 '11 02:12

Seth Carnegie


People also ask

Does Mingw contain g ++?

Mingw executables They also provide other compiler drivers, which in the case of mingw are executables such as g++, c++, g77, etc. These drivers invoke the appropriate compiler programs to compile your source code. Note that g++ and c++ are identical files except for the name difference.

Does stoi throw?

std::stoi may throw exceptions so it needs to be surrounded by try/catch. In applications where std::stoi may be used frequently, it could be useful to have a wrapper.


1 Answers

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.

like image 107
DRH Avatar answered Oct 13 '22 02:10

DRH