3

好吧,我想将我的 C++11 程序移植到 Windows,但在 mingw 4.7.1 中似乎没有实现 stoi 和 std::to_string 。我知道有人问过它并且有一个解决方案来编辑​​一些标题,但是在我的 mingw 版本(4.7.1 附带 codelite)中,标题是不同的,并且没有我必须移动的确切行(可能是因为答案是对于 mingw 4.6)。

所以我的问题是如何在 mingw 4.7 上获得这些功能?是否有任何指南可以在 4.7 中更改标题,或者它可能会包含在 4.8 中?

当然有 boost::lexical_cast,但我想保持我的代码不变,所以我正在寻找如何在 mingw 中启用这些功能的解决方案。

也许有一些自定义的 mingw 发行版支持这些功能?

4

1 回答 1

6

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
于 2013-02-07T21:46:30.580 回答