std::codecvt<InternT,ExternT,State>::encoding, do_encoding

< cpp‎ | locale‎ | codecvt
定义于头文件 <locale>
(1)
public:
int encoding() const throw();
(C++11 前)
public:
int encoding() const noexcept;
(C++11 起)
(2)
protected:
virtual int do_encoding() const throw();
(C++11 前)
protected:
virtual int do_encoding() const noexcept;
(C++11 起)
1) 公开成员函数,调用最终导出类的成员函数 do_encoding
2) 若此 codecvt 平面所表示的编码映射每个内部字符为相同的常数个外部字符,则返回该常数。若编码为变长(如 UTF-8 或 UTF-16 ),则返回 0 。若编码依赖平台,则返回 -1

返回值

对应一个 internT 字符的准确 externT 字符数。若该数变化则为 0 ,若编码依赖平台则为 -1

不转换特化 std::codecvt<char, char, std::mbstate_t> 返回 1

示例

#include <locale>
#include <iostream>
 
int main()
{
    std::cout << "en_US.utf8 is a variable-length encoding, encoding() returns "
              << std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                    std::locale("en_US.utf8")
              ).encoding() << '\n';
 
    std::cout << "zh_CN.gb18030 is also variable-length, encoding() == "
              << std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                    std::locale("zh_CN.gb18030")
              ).encoding() << '\n';
 
    std::cout << "ru_RU.koi8r is a single-byte encoding encoding() == "
              << std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                    std::locale("ru_RU.koi8r")
              ).encoding() << '\n';
}

输出:

en_US.utf8 is a variable-length encoding, encoding() returns 0
zh_CN.gb18030 is also variable-length, encoding() == 0
ru_RU.koi8r is a single-byte encoding encoding() == 1

参阅

MB_CUR_MAX
当前 C 本地环境中多字节字符的最大字节数
(宏变量)
返回能转换成单个 internT 字符的最大 externT 字符数
(虚受保护成员函数)