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

27,175

Solution 1

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.

Solution 2

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.

Share:
27,175
Seth Carnegie
Author by

Seth Carnegie

I have over 30 years of experience with C and C++, and experience with various other lesser languages. I highly recommend http://en.cppreference.com/w/cpp as an online C++11 Standard Library reference. Also, I have redefined the acronym STL to mean STandard Library. It is now safe to use again. Quotes Oli Charlesworth: Both these questions would be answered in the first few chapters of any decent introductory book on C++. jeremyskateboard: well i dont have a book The first rule of C/C++ is that there is no such thing as "C/C++". – Kerrek SB Best answers (of mine): How does Q_FOREACH macro work and why is it that complex? C++11: Move/Copy construction ambiguity? Are copy constructor elided when passing more than one function? How to declare an array of pointers to multidimensional arrays Integer range based template specialisation Variable declaration in the switch head? Error pasting “”HELLO“” and “”WORLD“” does not give a valid preprocessing token (because Raymond Chen made an edit to add a code sample) Favourite questions: gcc: Allow Undefined Symbols How can I make eclipse CDT ignore errors? Passing exceptions accross a C API boundary

Updated on July 14, 2022

Comments

  • Seth Carnegie
    Seth Carnegie almost 2 years

    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.