Namespaces
Variants

std::ctype<CharT>:: narrow, do_narrow

From cppreference.net
헤더 파일에 정의됨 <locale>
public :
char narrow ( CharT c, char dflt ) const ;
(1)
public :

const CharT * narrow ( const CharT * beg, const CharT * end,

char dflt, char * dst ) const ;
(2)
protected :
virtual char do_narrow ( CharT c, char dflt ) const ;
(3)
protected :

virtual const CharT * do_narrow ( const CharT * beg, const CharT * end,

char dflt, char * dst ) const ;
(4)
1,2) 공개 멤버 함수로, 가장 파생된 클래스의 해당 protected 가상 멤버 함수 do_narrow 오버로드를 호출합니다. 오버로드 (1)은 do_narrow ( c, dflt ) 를 호출하고, 오버로드 (2)는 do_narrow ( beg, end, dflt, dst ) 를 호출합니다.
3) (가능한 경우 넓은) 문자 c 를 멀티바이트 표현으로 변환합니다(예: UTF-8 인코딩에서 ASCII 문자는 단일 바이트임). 해당 변환이 존재하지 않을 경우 dflt 를 반환합니다.
4) 문자 배열 [ beg , end ) 범위의 모든 문자에 대해, 축소된 문자(또는 축소가 실패할 경우 dflt )를 dst 가 가리키는 문자 배열의 연속적인 위치에 기록합니다.

축소(narrowing)는 항상 성공하며, widen() 를 호출하여 항상 되돌릴 수 있습니다(모든 문자에 대해 기본 소스 문자 집합 (C++23까지) 기본 문자 집합 (C++23부터) 에 속하는 문자들에 대해).

축소(narrowing)가 성공하면, is() 가 인식하는 모든 문자 분류 범위를 보존합니다.

  • 즉, is ( m, c ) || ! ctc. is ( m, do_narrow ( c, dflt ) ) 은 항상 true 입니다. 이는 ctype 범주에 속하는 모든 명명된 ctype<char> 패싯 ctc ctype_base::mask m 에 대해 성립합니다 ( do_narrow dflt 를 반환하지 않는 경우).

어떤 숫자 문자를 축소 변환하면, 결과 값에서 문자 리터럴 '0' 을 뺀 차이가 원본 문자의 숫자 값과 동일함이 보장됩니다.

  • 즉, 임의의 숫자 문자 c 에 대해, ( do_narrow ( c, dflt ) - '0' ) 표현식은 해당 문자의 숫자 값을 평가합니다.

목차

매개변수

c - 변환할 문자
dflt - 변환이 실패할 경우 반환할 기본값
beg - 변환할 문자 배열의 첫 번째 문자를 가리키는 포인터
end - 변환할 문자 배열의 끝 다음을 가리키는 포인터
dst - 채울 문자 배열의 첫 번째 요소를 가리키는 포인터

반환값

1,3) 축소된 문자 또는 축소가 실패할 경우 dflt .
2,4) end

예제

#include <iostream>
#include <locale>
void try_narrow(const std::ctype<wchar_t>& f, wchar_t c)
{
    char n = f.narrow(c, 0);
    if (n)
        std::wcout << '\'' << c << "' narrowed to " << +(unsigned char)n << '\n';
    else
        std::wcout << '\'' << c << "' could not be narrowed\n";
}
int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    std::wcout.imbue(std::locale());
    std::wcout << std::hex << std::showbase << "In US English UTF-8 locale:\n";
    auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
    try_narrow(f, L'A');
    try_narrow(f, L'A');
    try_narrow(f, L'ě');
    std::locale::global(std::locale("cs_CZ.iso88592"));
    auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale());
    std::wcout << "In Czech ISO-8859-2 locale:\n";
    try_narrow(f2, L'A');
    try_narrow(f2, L'A');
    try_narrow(f2, L'ě');
}

가능한 출력:

In US English UTF-8 locale:
'A' narrowed to 0x41
'A' could not be narrowed
'ě' could not be narrowed
In Czech ISO-8859-2 locale:
'A' narrowed to 0x41
'A' could not be narrowed
'ě' narrowed to 0xec

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 126 C++98 1. 가역성을 나타내는 코드는
do_widen ( do_narrow ( c ) , 0 ) == c
2. 범주 보존을 나타내는 코드는
is ( m, c ) || ! ctc. is ( m, do_narrow ( c ) , dflt )
둘 다 수정됨
LWG 153 C++98 narrow 항상 오버로드 (4)를 호출함 해당 오버로드를 호출함

참고 항목

do_widen 을 호출합니다
(public member function)
문자를 좁힙니다
( std::basic_ios<CharT,Traits> 의 public member function)
가능한 경우 와이드 문자를 단일 바이트 좁은 문자로 변환합니다
(function)