stoi and std::to_string on mingw 4.7.1

stoi and std::to_string on mingw 4.7.1

本文关键字:mingw on and std to stoi string      更新时间:2023-10-16

我想把我的C++11程序移植到windows,但在第4.7.1页中似乎没有实现stoi和std::to_string。我知道有人问过我,有一个编辑一些标题的解决方案,但在我的mingw版本(与codelite一起发货的4.7.1)中,标题是不同的,并且没有我必须移动的确切行(可能是因为答案是针对mingw 4.6)

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

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

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

Mingw使用Windows API,而Windows没有提供用于实现to_stringvswprintf函数的一致版本,这要归咎于Microsoft。

如果你使用(非常)新版本的mingw-w64 fork和未发布的GCC 4.8版本,那么它会起作用,但你对主要的mingw32和GCC 4.7.1 不太满意

如果您愿意修补您的实现,您可以尝试上提供的解决方案http://tehsausage.com/mingw-to-string但要仔细阅读注意事项。

更新:

似乎只有std::to_wstring受到损坏的vswprintf函数的影响,所以我对GCC 4.9.3(及更高版本)进行了更改,它将为MinGW定义std::stoistd::stodstd::to_string等,而只保留未定义的to_wstring

如果你想自己编辑4.7.1标题,这里有相关的补丁:

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