Namespaces
Variants
Views
Actions

std::codecvt::encoding, std::codecvt::do_encoding

From cppreference.com
< cpp‎ | locale‎ | codecvt
Revision as of 20:51, 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 encoding() 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_encoding() 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_encoding of the most derived class.

2) if the encoding represented by this codecvt facet maps each internal character to the same, constant number of external characters, returns that number. If the encoding is variable-length (e.g. UTF-8 or UTF-16), returns 0. If the encoding is state-dependent, returns -1.

Contents

Return value

The exact number of externT characters that correspond to one internT character, if constant. 0 if the number varies, -1 if the encoding is state-dependent.

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

Exceptions

noexcept specification:  
noexcept
  

Example

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

Output:

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

See also

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