且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++0x 中的 Unicode 支持

更新时间:2022-10-25 12:40:32

Unicode 字符串文字支持 开始 在 GCC 4.5 中.也许这就是问题所在.

经过一些挖掘,我发现这个新的 unicode 文字的流在 N2035 它是 包括在标准草案中.根据此文档,您需要 u32ofstream 来输出字符串,但 GCC 4.5 C++0x 库中不存在此类.

作为一种解决方法,您可以使用普通的 fstream:

std::ofstream fout2("output2.txt", std::ios::out | std::ios::binary);fout2.write((const char *)str.c_str(), str.size() * 4);

这样我就在我的 Intel 机器上以 UTF-32LE 格式输出你的字符串(小端).

我对 u32ofstream 的状态有点错误:根据 最新草案关于 C++ 标准委员会的网站 你必须像你一样使用 std::basic_ofstream.此类将使用必须在标准库中实现的 codecvt<char32_t,char,typename traits::state_type> 类(参见 §27.9.1.1 末尾)(搜索 codecvt<char32_t 在文档中),但它在 GCC 4.5 中不可用.

I'm trying to use new unicode characters in C++0x. So I wrote sample code:

#include <fstream>
#include <string>
int main()
{
    std::u32string str = U"Hello World";

    std::basic_ofstream<char32_t> fout("output.txt");

    fout<<str;  
    return 0;
}

But after executing this program I'm getting empty output.txt file. So why it's not printing Hello World?

Also is there something like a cout and cin already defined for these types, or stdin and stdout doesn't support Unicode?

Edit: I'm using g++ and Linux.

EDIT:АТТЕNTION. I have discovered, that standard committee dismissed Unicode streams from C++0x. So previously accepted answer is not correct anymore. For more information see my answer!

Unicode string literals support began in GCC 4.5. Maybe that's the problem.

[edit]

After some digging I've found that streams for this new unicode literals are described in N2035 and it was included in a draft of the standard. According to this document you need u32ofstream to output you string but this class is absent in GCC 4.5 C++0x library.

As a workaround you can use ordinary fstream:

std::ofstream fout2("output2.txt", std::ios::out | std::ios::binary);
fout2.write((const char *)str.c_str(), str.size() * 4);

This way I've output your string in UTF-32LE on my Intel machine (which is little-endian).

[edit]

I was a little bit wrong about the status of u32ofstream: according to the latest draft on the The C++ Standards Committee's web site you have to use std::basic_ofstream<char32_t> as you did. This class would use codecvt<char32_t,char,typename traits::state_type> class (see end of §27.9.1.1) which has to be implemented in the standard library (search codecvt<char32_t in the document), but it's not available in GCC 4.5.