stoi and std::to_string on mingw 4.7.1 [duplicate]

Well I wanted to port my C++11 programm to windows, but it seems in mingw 4.7.1 there is no stoi and std::to_string implemented. I know it has been asked and there was a solution to edit some header, but in my mingw version (4.7.1 shipped with codelite) the header is different and there are no the exact lines I have to move (probably because the answer was for mingw 4.6).

So my question is how can I get these functions on mingw 4.7? Is there any guide what to change in header in 4.7 or maybe it will be included in 4.8?

Of course there is boost::lexical_cast, but I'd like to keep my code unchanged, so I'm looking for solution how to enable these functions in mingw.

Maybe there is some custom mingw distro which comes with support for these functions?


Solution 1:

Mingw uses the Windows API, and Windows doesn't provide a conforming version of the vswprintf function used to implement to_string, blame Microsoft.

If you use a (very) recent version of the mingw-w64 fork and the unreleased 4.8 version of GCC then it will work, but you're outta luck with the main mingw32 and GCC 4.7.1

If you're willing to patch your implementation you could try the solution given at http://tehsausage.com/mingw-to-string but read the caveats carefully.

Update:

It seems that only std::to_wstring is affected by the broken vswprintf function, so I've made a change for GCC 4.9.3 (and later versions) which will define std::stoi, std::stod, std::to_string etc. for MinGW, and just leave to_wstring undefined.

If you want to edit the 4.7.1 header yourself, here's the relevant patch:

--- a/home/jwakely/gcc/4.7.1/include/c++/4.7.1/bits/basic_string.h
+++ b/home/jwakely/gcc/4.7.1/include/c++/4.7.1/bits/basic_string.h.fix
@@ -2808,8 +2808,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace

-#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
-     && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
+#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99)

 #include <ext/string_conversions.h>

@@ -2959,6 +2958,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   stold(const wstring& __str, size_t* __idx = 0)
   { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }

+#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
   // DR 1261.
   inline wstring
   to_wstring(int __val)
@@ -3021,6 +3021,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                                            L"%Lf", __val);
   }
 #endif
+#endif

 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace