名前空間
変種
操作

std::ctype<CharT>::widen, do_widen

From cppreference.com
< cpp‎ | locale‎ | ctype
 
 
 
 
 
ヘッダー <locale> で定義
public:
CharT widen( char c ) const;
(1)
public:
const char* widen( const char* beg, const char* end, CharT* dst ) const;
(2)
protected:
virtual CharT do_widen( char c ) const;
(3)
protected:
virtual const char* do_widen( const char* beg, const char* end, CharT* dst ) const;
(4)
1,2) 公開メンバ関数。最も派生したクラスの対応する保護仮想メンバ関数do_widenオーバーロードを呼び出します。オーバーロード(1)はdo_widen(c)を呼び出し、オーバーロード(2)はdo_widen(beg, end, dst)を呼び出します。
3) 単一バイト文字cを、最も単純で合理的な変換を使用して、対応するワイド文字表現に変換します。通常、これはマルチバイトエンコーディングが単一バイトである文字(例: UTF-8のU+0000-U+007F)にのみ適用されます。
4) 文字配列[begend)内の各文字について、対応するワイド化された文字を、dstが指す文字配列の連続した場所に書き込みます。

ワイド化は常にワイド文字を返しますが、基本的なソース文字セット(C++23まで)基本的な文字セット(C++23以降)からの文字のみが一意で明確に定義されたワイド化変換を持つことが保証されており、それはまたnarrow()による逆変換も保証されています。実際には、マルチバイト表現が単一バイトであるすべての文字は通常、対応するワイド文字にワイド化され、可能な単一バイト値の残りは通常、同じプレースホルダ値、通常はCharT(-1)にマッピングされます。

ワイド化は、成功した場合、is()に知られているすべての文字分類カテゴリを保持します。

目次

[編集] パラメータ

c - 変換する文字
dflt - 変換が失敗した場合に生成されるデフォルト値
beg - 変換する文字配列の最初の文字へのポインタ
end - 変換する文字配列の終端の次を指すポインタ
dst - 埋め込む文字配列の最初の要素へのポインタ

[編集] 戻り値

1,3) ワイド化された文字。
2,4) end

[編集]

#include <iostream>
#include <locale>
 
void try_widen(const std::ctype<wchar_t>& f, char c)
{
    wchar_t w = f.widen(c);
    std::cout << "The single-byte character " << +(unsigned char)c
              << " widens to " << +w << '\n';
}
 
int main()
{
    std::locale::global(std::locale("cs_CZ.iso88592"));
    auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
    std::cout << std::hex << std::showbase << "In Czech ISO-8859-2 locale:\n";
    try_widen(f, 'a');
    try_widen(f, '\xdf'); // German letter ß (U+00df) in ISO-8859-2
    try_widen(f, '\xec'); // Czech letter ě (U+011b) in ISO-8859-2
 
    std::locale::global(std::locale("cs_CZ.utf8"));
    auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale());
    std::cout << "In Czech UTF-8 locale:\n";
    try_widen(f2, 'a');
    try_widen(f2, '\xdf'); 
    try_widen(f2, '\xec'); 
}

実行結果の例

In Czech ISO-8859-2 locale:
The single-byte character 0x61 widens to 0x61
The single-byte character 0xdf widens to 0xdf
The single-byte character 0xec widens to 0x11b
In Czech UTF-8 locale:
The single-byte character 0x61 widens to 0x61
The single-byte character 0xdf widens to 0xffffffff
The single-byte character 0xec widens to 0xffffffff

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 153 C++98 widenは常にオーバーロード(4)を呼び出す 対応するオーバーロードを呼び出す

[編集] 関連項目

do_narrowを呼び出します。
(公開メンバ関数) [編集]
文字をワイド化する
(std::basic_ios<CharT,Traits> の public メンバ関数) [編集]
可能であれば、1バイトのナロー文字をワイド文字にワイド化する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)