2010-04-26 81 views
8

由于Windows没有默认支持UTF8的C++语言环境,因此我想构造一个支持UTF8的自定义语言环境对象(通过使用自定义ctype构面创建它)。是否有可能创建自己的自定义区域设置

我怎样才能地区构造对象与我自己的CTYPE实现(我只发现功能来构建使用已经存在的区域作为底部的区域。)

如果C++不支持的语言环境的建设有一个自定义的ctype facet,为什么会这样呢?

+1

ctype与utf-8等可变长度编码不兼容。例如,tolower()接受并返回一个CharType。因此ASCII子集之外的任何字符都将不能被ctype转换。 – 2010-04-26 13:30:48

+2

请参阅http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/codecvt.html。 Stroustrup附录D,http://www.research.att.com/~bs/3rd_loc.pdf。和标准的C + + IOStreams和语言环境http://books.google.fr/books?id=nnRog4I-3jwC – anno 2010-04-26 14:29:33

+0

@anno:感谢这些链接帮助我很多,+1 – smerlin 2010-04-26 20:02:39

回答

5

可以通过从std :: locale :: facet继承来创建自定义构面。区域设置可以使用以下代码中的自定义构面:

class custom_facet : public std::locale::facet { 
public: 
    static std::locale::id id; 
    custom_facet(int); 
    int custom_value() const; 
    }; 

std::locale custom_locale (std::locale(), new custom_facet()); 
int s = std::use_facet<custom_facet>(custom_locale).custom_value(); 
+0

被接受为答案,即使这只是部分回答了我的问题,但是annos链接也帮助了我,所以这个问题对我来说已经解决了。 – smerlin 2010-04-26 20:05:19