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

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

Leave a Comment