std::codecvt::max_length, do_max_length

来自cppreference.com
< cpp‎ | locale‎ | codecvt
定义于头文件 <locale>
public:
int max_length() const;
(1)
protected:
virtual int do_max_length() const;
(2)
1) 公开成员函数,调用最终导出类的成员函数 do_max_length
2) 返回对于任何合法范围 [from, from_end) 和任何 state 合法 , do_length(state, from, from_end, 1) 所能返回的最大值。

目录

[编辑] 返回值

若以 in() 转换生成一个 InternT 字符,能消耗的最大 ExternT 字符数。

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

[编辑] 注意

若编码依赖状态( encoding() == -1 ),则可能消耗多于 max_length() 个外部字符生成一个内部字符。

[编辑] 异常

(无) (C++11 前)
noexcept 规定:  
noexcept
  
(C++11 起)

[编辑] 示例

#include <locale>
#include <iostream>
#include <codecvt>
 
int main()
{
    std::cout << "In codecvt_utf8, the longest multibyte character is "
              << std::codecvt_utf8<wchar_t>().max_length() << " bytes long\n";
 
    std::cout << "In header-consuming codecvt_utf8, the longest multibyte character is "
              << std::codecvt_utf8<wchar_t,
                                   0x10ffff,
                                   std::consume_header>().max_length() << " bytes long\n";
 
    std::cout << "In this system's en_US.utf8, the longest multibyte character is "
              << std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                    std::locale("en_US.utf8")
              ).max_length() << " bytes long\n";
 
    std::cout << "In this system's zh_CN.gb18030, the longest multibyte character is "
              << std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                    std::locale("zh_CN.gb18030")
              ).max_length() << " bytes long\n";
}

输出:

In codecvt_utf8, the longest multibyte character is 4 bytes long
In header-consuming codecvt_utf8, the longest multibyte character is 7 bytes long
In this system's en_US.utf8, the longest multibyte character is 6 bytes long
In this system's zh_CN.gb18030, the longest multibyte character is 4 bytes long

[编辑] 参阅

MB_CUR_MAX
当前 C 本地环境中多字节字符的最大字节数
(宏变量)[编辑]
返回产生一个 internT 字符所需的 externT 字符数,若此值为常数
(虚受保护成员函数) [编辑]