Namespaces
Variants

std:: locale

From cppreference.net
헤더 파일에 정의됨 <locale>
class locale ;

클래스 std::locale 의 객체는 불변의 패싯들로 구성된 불변의 인덱스 집합입니다. C++ 입출력 라이브러리의 각 스트림 객체는 std::locale 객체와 연관되어 있으며, 모든 데이터의 파싱과 포맷팅에 해당 패싯들을 사용합니다. 또한, 각 std::basic_regex 객체에도 로케일 객체가 연관되어 있습니다. (C++11부터) 로케일 객체는 표준 컨테이너 및 알고리즘과 함께 문자열 비교를 수행하는 predicate로 사용될 수도 있으며, 보유한 패싯을 직접 획득하거나 수정하기 위해 접근할 수 있습니다.

C++ 프로그램에서 생성된 각 로케일은 최소한 다음과 같은 표준 패싯을 보유합니다(즉, std::has_facet 이 모든 패싯 타입에 대해 true 를 반환함). 그러나 프로그램은 추가적인 특수화 또는 완전히 새로운 패싯을 정의하고 이를 기존 로케일 객체에 추가할 수 있습니다.

지원되는 패싯
std:: ctype < char >
std:: ctype < wchar_t >
std:: codecvt < char , char , std:: mbstate_t >
std:: codecvt < wchar_t , char , std:: mbstate_t >
std:: num_get < char >
std:: num_get < wchar_t >
std:: numpunct < char >
std:: numpunct < wchar_t >
std:: num_put < char >
std:: num_put < wchar_t >
std:: money_get < char >
std:: money_get < wchar_t >
std:: moneypunct < char >
std:: moneypunct < char , true >
std:: moneypunct < wchar_t >
std:: moneypunct < wchar_t , true >
std:: money_put < char >
std:: money_put < wchar_t >
std:: time_get < char >
std:: time_get < wchar_t >
std:: collate < char >
std:: collate < wchar_t >
std:: time_put < char >
std:: time_put < wchar_t >
std:: messages < char >
std:: messages < wchar_t >
사용 중단된 패싯
std:: codecvt < char16_t , char , std:: mbstate_t > (C++11부터) (C++20에서 사용 중단됨)
std:: codecvt < char32_t , char , std:: mbstate_t > (C++11부터) (C++20에서 사용 중단됨)
std:: codecvt < char16_t , char8_t, std:: mbstate_t > (C++20부터) (사용 중단됨)
std:: codecvt < char32_t , char8_t, std:: mbstate_t > (C++20부터) (사용 중단됨)

내부적으로 locale 객체는 참조 카운트가 적용된 포인터 배열(인덱스는 std::locale::id 로 지정됨)을 참조 카운트 방식으로 가리키는 포인터처럼 구현됩니다: locale을 복사하는 것은 단일 포인터만 복사하고 여러 참조 카운트를 증가시키는 작업입니다. 표준 C++ 라이브러리의 스레드 안전성 보장(서로 다른 객체에 대한 연산은 항상 스레드 안전함)을 유지하기 위해, locale 참조 카운트와 각 facet 참조 카운트 모두 std::shared_ptr 과 유사한 방식으로 스레드 안전하게 업데이트됩니다.

목차

멤버 타입

유형 설명
패싯 인덱스 유형: 각 패싯 클래스는 이 유형의 public static 멤버를 선언하거나 상속받아야 함
(클래스)
모든 패싯 카테고리의 기본 클래스: 모든 카테고리의 각 패싯은 이 유형에서 파생됨
(클래스)
category
int
(typedef)

멤버 상수

이름 설명
const category none
[static]
패싯 범주가 없음을 나타내는 제로 값
(public static member constant)
const category collate
[static]
collate 패싯 범주를 나타내는 비트마스크 값
(public static member constant)
const category ctype
[static]
ctype 패싯 범주를 나타내는 비트마스크 값
(public static member constant)
const category monetary
[static]
monetary 패싯 범주를 나타내는 비트마스크 값
(public static member constant)
const category numeric
[static]
numeric 패싯 범주를 나타내는 비트마스크 값
(public static member constant)
const category time
[static]
time 패싯 범주를 나타내는 비트마스크 값
(public static member constant)
const category messages
[static]
messages 패싯 범주를 나타내는 비트마스크 값
(public static member constant)
const category all
[static]
collate | ctype | monetary | numeric | time | messages
(public static member constant)

std::locale 멤버 함수에서 category 인자를 기대하는 경우, 위에서 정의된 카테고리 값들 중 하나 또는 두 개 이상의 값을 합친 값을 요구합니다. LC 상수들 은 허용되지 않습니다.

멤버 함수

새로운 locale을 생성합니다
(public member function)
locale을 파괴하고 참조 카운트가 0이 되는 facet들을 파괴합니다
(public member function)
locale을 대체합니다
(public member function)
다른 locale에서 컴파일 타임에 식별된 facet을 복사하여 locale을 생성합니다
(public member function)
locale의 이름을 반환하거나 이름이 없는 경우 "*" 를 반환합니다
(public member function)
(C++26)
locale과 연관된 문자 인코딩 방식을 반환합니다
(public member function)
(removed in C++20)
locale 객체 간의 동등성 비교
(public member function)
이 locale의 collate facet을 사용하여 두 문자열을 사전식으로 비교합니다
(public member function)
[static]
전역 locale을 변경합니다
(public static member function)
[static]
"C" locale에 대한 참조를 얻습니다
(public static member function)

예제

로케일-민감한 프로그램의 일반적인 프롤로그를 보여줍니다 (크로스-플랫폼).

#include <iostream>
#include <locale>
int main()
{
    std::wcout << L"User-preferred locale setting is "
               << std::locale("").name().c_str() << L'\n';
    // on startup, the global locale is the "C" locale
    std::wcout << 1000.01 << L'\n';
    // replace the C++ global locale and the "C" locale with the user-preferred locale
    std::locale::global(std::locale(""));
    // use the new global locale for future wide character output
    std::wcout.imbue(std::locale());
    // output the same number again
    std::wcout << 1000.01 << L'\n';
}

가능한 출력:

User-preferred locale setting is en_US.UTF8
1000.01
1,000.01

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 340 C++98 모든 로캘이 보유해야 하는 표준 패싯 집합이 불명확했음 명확하게 규정됨
LWG 347 C++98 category 타입 매개변수가 LC 상수를 수용할 수 있었음 더 이상 수용되지 않음

참고 항목

IANA 문자 집합 레지스트리 에 접근하기 위한 인터페이스를 설명합니다
(클래스)
로케일에서 패싯을 획득합니다
(함수 템플릿)
로케일이 특정 패싯을 구현하는지 확인합니다
(함수 템플릿)
로케일을 설정합니다
( std::ios_base 의 public 멤버 함수)
현재 로케일을 반환합니다
( std::ios_base 의 public 멤버 함수)