简体   繁体   English

codecvt_utf8 <wchar_t> 转换为本地编解码器 <wchar_t, char>

[英]codecvt_utf8<wchar_t> gets converted to native codecvt<wchar_t, char>

I have the following code as a private function in a string class I'm writing (it's "borrowed" from wstring_convert with some modifications done by me): 我在编写的字符串类中将以下代码作为私有函数使用(它是从wstring_convert借来的,并由我做了一些修改):

template <typename T>
std::string my_string::to_bytes(const typename T::intern_type *_First, const typename T::intern_type *_Last) const
{
    const T* p_conv = &std::use_facet<T>(m_loc);
    ...
    switch (p_conv->out(_State0, _First, _Last, _First, _Dest, _Dest + _Bbuf.size(), _Dnext))
    {
        ...
    }
}

m_loc is a global std::locale containing the system locale. m_loc是一个全局std :: locale,包含系统区域设置。

The function as such is not the problem, the problem is that when I call the function like this: 这样的函数不是问题,问题是当我这样调用函数时:

const wchar_t *_Wptr = wstr.c_str();
m_utf8 += to_bytes<codecvt_utf8<wchar_t>>(_Wptr, _Wptr + wstr.size());

it doesn't actually use codecvt_utf8<wchar_t> (which converts between native wchar_t and utf-8) but instead codecvt<wchar_t, char> (which converts between native wchar_t and native char). 它实际上并没有使用codecvt_utf8<wchar_t> (在本机wchar_t和utf-8之间转换),而是使用了codecvt<wchar_t, char> (在本机wchar_t和本机char之间转换)。 I can't for the life of me understand why this happens. 我一生无法理解为什么会这样。 Obviously I must do something wrong somewhere and I can admit the locale system with all its facets and whatnots is confusing me a bit. 显然,我必须在某个地方做些错误的事情,并且我可以接受语言环境系统的所有方面,这让我有些困惑。

I guess it's a problem related to use_facet and my locale in some way. 我想这在某种程度上与use_facet和我的语言环境有关。 Maybe the locale can't create the utf-8 facet? 也许语言环境无法创建utf-8构面? If that sentence makes sense? 那句话说得通吗? Maybe I'm not using use_facet correctly? 也许我没有正确使用use_facet?

codecvt_utf8 is a standalone, locale-independent facet, provided by the standard library. codecvt_utf8是标准库提供的独立的,与语言环境无关的构面。 There is no need to use use_facet to access it. 无需使用use_facet进行访问。

What you get from use_facet<T> is whatever facet is in your locale with the id T::id, which happens to be its codecvt use_facet<T>中获得的内容是您的语言环境中ID为T :: id的任何方面,恰好是其编解码器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM