Namespaces
Variants
Views
Actions

std::codecvt::max_length, std::codecvt::do_max_length

From cppreference.com
< cpp‎ | locale‎ | codecvt
Revision as of 20:48, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
public:
int max_length() const
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
protected:
int do_max_length() const
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1) public member function, calls the member function do_max_length of the most derived class.

2) returns the maximum value that do_length(state, from, from_end, 1) can return for any valid range [from, from_end) and any valid state.

Contents

Return value

The maximum number of externT characters that could be consumed if converted by Template:rlpf to produce one internT character.

The non-converting specialization std::codecvt<char, char, std::mbstate_t> returns 1

Notes

If the encoding is state-dependent (encoding() == -1), then more than max_length() external characters may be consumed to produce one internal character

Exceptions

noexcept specification:  
noexcept
  

Example

#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";
}

Output:

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

See also

Template:cpp/string/multibyte/dcl list MB CUR MAXTemplate:cpp/locale/codecvt/dcl list do encoding