是否可以创建自己的自定义区域设置

sme*_*lin 9 c++ locale

由于Windows默认情况下没有支持UTF8的C++语言环境,我想构建一个支持UTF8的自定义语言环境对象(通过使用自定义ctype facet创建它).

如何使用我自己的ctype实现构造一个locale对象(我只找到使用已经存在的语言环境作为基础来构造语言环境的函数.)

如果C++根本不支持使用自定义ctype facet构建语言环境,为什么会这样呢?

小智 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();
Run Code Online (Sandbox Code Playgroud)