将 std::wstring 移植到 gcc cygwin

porting std::wstring to gcc cygwin

我正在尝试将 Visual Studio 2010 年编写的项目从 Microsoft 编译器移植到 gcc Cygwin。我的问题是 gcc Cygwin 或 gcc 支持 std::wstring 吗?有解决办法吗?

是的。支持,但不容易。

To support wstring, your platform needs to support a whole bunch of wchar_t functionality. q.v. Standard C++ IOStreams and Locales by Langer and Kreft.

It's really not enough to say "Hey, we've got wchar_t, and wchar_t character traits, so we can turn on std::wstring." The platform also needs to provide support for the C++ wide character I/O: wistream, wostream, facet, cvt, locale. And that's where things become problematic.

For Cygwin on Windows, the platform is Cygwin (and its amazingly cool POSIX API layer) moreso than the WinAPI.

ASIDE: I'm not sure what the MinGW situation is, but since MinGW uses WinAPI directly rather than having a more Unix-like POSIX API as the approach, MinGW may not be in this Cygwin situation.

Windows does support that functionality, but it appears that no one has gone through the considerable effort of plumbing up Cygwin to use those facilities. (Those are the FooW routines, rather than the FooA routines, in the WinAPI. And there is a lot of work to get the locale magic to work correctly.)

Also, Windows uses a wchar_t of 2 bytes. (Previous UCS-2, from the Unicode 1.0 era. Now with Vista and Win7, it's UTF-16.) It would be a bit retro to have Cygwin use a 2 byte wchar_t, rather than a 4 byte wchar_t. That just makes everything a little more difficult.

有关详细信息,请转到 gcc.gnu.org/ml/gcc-help:

确实有支持。此代码在 g++ 4.8 上编译良好(在任何符合标准的编译器上都应该如此):

#include <iostream>
#include <string>

int main(){
  std::wstring s(L"Hello World!");
  std::wcout << s;
}