Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/locale/locale"

From cppreference.com
< cpp‎ | locale‎ | locale
(oh, I forgot the params, didn't I? Also category is the type, so +argument name)
m (Example: "")
 
(26 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{cpp/locale/locale/title | locale}}
+
{{cpp/locale/locale/title|locale}}
{{cpp/locale/locale/sidebar}}
+
{{cpp/locale/locale/navbar}}
  
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | locale}}
+
{{dcl rev multi|num=1|until1=c++11
{{ddcl list item | num=1 | 1=
+
|dcl1=
locale();
+
locale() throw();
 +
|dcl2=
 +
locale() noexcept;
 
}}
 
}}
{{ddcl list item | num=2 | 1=
+
{{dcl rev multi|num=2|until1=c++11
locale( const locale& other );
+
|dcl1=
 +
locale( const locale& other ) throw();
 +
|dcl2=
 +
locale( const locale& other ) noexcept;
 
}}
 
}}
{{ddcl list item | num=3 | 1=
+
{{dcl|num=3|
 
explicit locale( const char* std_name );
 
explicit locale( const char* std_name );
 
}}
 
}}
{{ddcl list item | num=4 | 1=
+
{{dcl|num=4|since=c++11|
 
explicit locale( const std::string& std_name );
 
explicit locale( const std::string& std_name );
 
}}
 
}}
{{ddcl list item | num=5 | 1=
+
{{dcl|num=5|
locale( const locale& other, const char* std_name, category cat );
+
locale( const locale& other, const char* std_name, category cats );
 
}}
 
}}
{{ddcl list item | num=6 | 1=
+
{{dcl|num=6|since=c++11|
locale( const locale& other, const std::string& std_name, category cat );
+
locale( const locale& other, const std::string& std_name, category cats );
 
}}
 
}}
{{ddcl list item | num=7 | 1=
+
{{dcla|num=7|
 
template< class Facet >
 
template< class Facet >
 
locale( const locale& other, Facet* f );
 
locale( const locale& other, Facet* f );
 
}}
 
}}
{{ddcl list item | num=8 | 1=
+
{{dcl|num=8|
locale( const locale& other, const locale& one, category cat );
+
locale( const locale& other, const locale& one, category cats );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
1) Default constructor. Constructs a copy of the global C++ locale (that is, the locale most recently used as the argument to {{cpp|std::locale::global}} or a copy of {{cpp|std::locale::classic}} if no call to {{cpp|std::locale::global}} has been made.
+
Constructs a new locale object.
  
2) Copy constructor. Constructs a copy of {{tt|other}}.
+
@1@ Default constructor. Constructs a copy of the global C++ locale, which is the locale most recently used as the argument to {{lc|std::locale::global}} or a copy of {{c|std::locale::classic()}} if no call to {{lc|std::locale::global}} has been made.
  
3-4) Constructs a copy of the system locale with specified {{tt|std_name}} (such as "C", or "POSIX", or "en_US.UTF-8", or "English_US.1251"), if such locale is supported by the operating system. The locale constructed in this manner has a name.
+
@2@ Copy constructor. Constructs a copy of {{c|other}}.
  
5-6) Constructs a copy of {{tt|other}} except for all the facets identified by the {{tt|cat}} argument, which are copied from the system locale identified by its {{tt|std_name}}. The locale constructed in this manner has the same name as {{tt|other}}.
+
@3,4@ Constructs a copy of the system locale with specified {{c|std_name}} (such as {{c|"C"}}, or {{c|"POSIX"}}, or {{c|"en_US.UTF-8"}}, or {{c|"English_US.1251"}}), if such locale is supported by the operating system. The locale constructed in this manner has a name.
  
7) Constructs a copy of {{tt|other}} except for the facet of type {{tt|Facet}} (typically deduced from the type of the argument) which is installed from the argument {{tt|facet}}. If {{tt|facet}} is {{cpp|NULL}}, the constructed locale is a full copy of {{tt|other}}. The locale constructed in this manner has no name.
+
@4@ Equivalent to {{c|locale(std_name.c_str())}}.
  
8) Constructs a copy of {{tt|other}} except for all the facets identified by the {{tt|cat}} argument, which are copied from {{tt|one}}. If both {{tt|other}} and {{tt|one}} have names, then the resulting locale also has a name.
+
@5@ Constructs a copy of {{c|other}} except for all the facets identified by the {{c|cats}} argument, which are copied from the system locale identified by its {{c|std_name}}. The locale constructed in this manner has a name if and only if {{c|other}} has a name.
  
===Parameters===
+
@6@ Equivalent to {{c|locale(other, std_name.c_str(), cats)}}.
{{param list begin}}
+
{{param list item | other | another locale to copy}}
+
{{param list item | std_name | name of the system locale to use}}
+
{{param list item | f | pointer to a facet to merge with {{tt|other}} }}
+
{{param list item | cat | the locale category used to identify the facets to merge with {{tt|other}} }}
+
{{param list item | one | another locale to take facets from}}
+
{{param list end}}
+
  
===Exceptions===
+
@7@ Constructs a copy of {{c|other}} except for the facet of type {{tt|Facet}} (typically deduced from the type of the argument) which is installed from {{c|f}}. If {{c|f}} is a null pointer, the constructed locale is a full copy of {{c|other}}. The program is ill-formed if {{tt|Facet}} is not a {{rlp|facet#Facet class|facet}} or it is a volatile-qualified facet.
1-2) {{noexcept}}
+
  
3) {{cpp|std::runtime_error}} if the operating system has no locale named {{tt|std_name}} or if {{tt|std_name}} is {{cpp|NULL}}.
+
@@ If {{c|f}} is null, the constructed locale has the same name as {{c|other}}. Otherwise, the constructed locale has no name.
  
4) {{cpp|std::runtime_error}} if the operating system has no locale named {{tt|std_name}}.
+
@8@ Constructs a copy of {{c|other}} except for all the facets identified by the {{c|cats}} argument, which are copied from {{c|one}}.
  
5) {{cpp|std::runtime_error}} if the operating system has no locale named {{tt|std_name}} or if {{tt|std_name}} is {{cpp|NULL}}.
+
@@ If {{c|cats}} is equal to {{tt|locale::none}}, the constructed locale has a name if and only if {{c|other}} has a name. Otherwise, the constructed locale has a name if and only if {{c|other}} and {{c|one}} both have names.
  
6) {{cpp|std::runtime_error}} if the operating system has no locale named {{tt|std_name}}.
+
===Parameters===
 +
{{par begin}}
 +
{{par|other|another locale to copy}}
 +
{{par|std_name|name of the system locale to use}}
 +
{{par|f|pointer to a facet to merge with {{c|other}}}}
 +
{{par|cats|the facet categories used to identify the facets to merge with {{c|other}}}}
 +
{{par|one|another locale to take facets from}}
 +
{{par end}}
  
7-8) (none)
+
===Exceptions===
 +
@3,5@ {{lc|std::runtime_error}} if the operating system has no locale named {{c|std_name}} or if {{c|std_name}} is a null pointer.
 +
 
 +
@4,6@ {{lc|std::runtime_error}} if the operating system has no locale named {{c|std_name}}.
 +
 
 +
===Notes===
 +
Overload {{vl|7}} is typically called with its second argument, {{c|f}}, obtained directly from a new-expression: the locale is responsible for calling the matching {{c/core|delete}} from its own destructor.
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
|
+
|code=
| code=
+
#include <codecvt>
 
#include <iostream>
 
#include <iostream>
 
#include <locale>
 
#include <locale>
#include <codecvt>
+
 
 +
std::ostream& operator<< (std::ostream& os, const std::locale& loc)
 +
{
 +
    if (loc.name().length() <= 80)
 +
        os << loc.name();
 +
    else
 +
        for (const auto c : loc.name())
 +
            os << c << (c == ';' ? "\n  " : "");
 +
 
 +
    return os << '\n';
 +
}
  
 
int main()
 
int main()
 
{
 
{
     std::locale l1; // l1 is a copy of the classic "C" locale
+
     std::locale l1;
     std::locale l2("en_US.UTF-8"); // l2 is a unicode locale
+
    std::cout << "Name of a copy of the classic \"C\" locale: " << l1;
     std::locale l3(l1, l2, std::locale::ctype); // l3 is "C" except for ctype, which is unicode
+
 
     std::locale l4(l1, new std::codecvt_utf8<wchar_t>); // l4 is "C" except for codecvt
+
     std::locale l2("en_US.UTF-8");
     std::cout << "Locale names:\nl1: " << l1.name() << "\nl2: " << l2.name()
+
     std::cout << "Name of unicode locale: " << l2;
              << "\nl3: " << l3.name() << "\nl4: " << l4.name() << '\n';
+
 
 +
     std::locale l3(l1, new std::codecvt_utf8<wchar_t>);
 +
     std::cout << "Name of \"C\" locale except for codecvt: " << l3;
 +
 
 +
    std::locale l4(l1, l2, std::locale::ctype);
 +
    std::cout << "Name of \"C\" locale except for ctype, which is unicode:\n  " << l4;
 
}
 
}
| output=
+
|p=true
Locale names:
+
|output=
l1: C
+
Name of a copy of the classic "C" locale: C
l2: en_US.UTF-8
+
Name of unicode locale: en_US.UTF-8
l3: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C
+
Name of "C" locale except for codecvt: *
l4: *
+
Name of "C" locale except for ctype, which is unicode:
 +
  LC_CTYPE=en_US.UTF-8;
 +
  LC_NUMERIC=C;
 +
  LC_TIME=C;
 +
  LC_COLLATE=C;
 +
  LC_MONETARY=C;
 +
  LC_MESSAGES=C;
 +
  LC_PAPER=C;
 +
  LC_NAME=C;
 +
  LC_ADDRESS=C;
 +
  LC_TELEPHONE=C;
 +
  LC_MEASUREMENT=C;
 +
  LC_IDENTIFICATION=C
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=436|std=C++98|before=for overload {{vl|7}}, it was unclear<br>whether {{tt|Facet}} can be cv-qualified|after=it can be const-qualified,<br>but not volatile-qualified}}
 +
{{dr list item|wg=lwg|dr=2295|std=C++98|before=for overload {{vl|7}}, the constructed locale had no name even if {{c|f}} is null|after=it has the name of {{c|other}}}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/locale/locale/dcl list ~locale}}
+
{{dsc inc|cpp/locale/locale/dsc ~locale}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
===External links===
 +
{{elink begin}}
 +
{{elink|num=1|1=[https://ss64.com/locale.html List of Windows locale names].}}
 +
{{elink|num=2|1=[https://lh.2xlibre.net/locales/ List of Linux locale names].}}
 +
{{elink end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 17:51, 21 October 2024

 
 
 
 
(1)
locale() throw();
(until C++11)
locale() noexcept;
(since C++11)
(2)
locale( const locale& other ) throw();
(until C++11)
locale( const locale& other ) noexcept;
(since C++11)
explicit locale( const char* std_name );
(3)
explicit locale( const std::string& std_name );
(4) (since C++11)
locale( const locale& other, const char* std_name, category cats );
(5)
locale( const locale& other, const std::string& std_name, category cats );
(6) (since C++11)
template< class Facet >
locale( const locale& other, Facet* f );
(7)
locale( const locale& other, const locale& one, category cats );
(8)

Constructs a new locale object.

1) Default constructor. Constructs a copy of the global C++ locale, which is the locale most recently used as the argument to std::locale::global or a copy of std::locale::classic() if no call to std::locale::global has been made.
2) Copy constructor. Constructs a copy of other.
3,4) Constructs a copy of the system locale with specified std_name (such as "C", or "POSIX", or "en_US.UTF-8", or "English_US.1251"), if such locale is supported by the operating system. The locale constructed in this manner has a name.
4) Equivalent to locale(std_name.c_str()).
5) Constructs a copy of other except for all the facets identified by the cats argument, which are copied from the system locale identified by its std_name. The locale constructed in this manner has a name if and only if other has a name.
6) Equivalent to locale(other, std_name.c_str(), cats).
7) Constructs a copy of other except for the facet of type Facet (typically deduced from the type of the argument) which is installed from f. If f is a null pointer, the constructed locale is a full copy of other. The program is ill-formed if Facet is not a facet or it is a volatile-qualified facet.
If f is null, the constructed locale has the same name as other. Otherwise, the constructed locale has no name.
8) Constructs a copy of other except for all the facets identified by the cats argument, which are copied from one.
If cats is equal to locale::none, the constructed locale has a name if and only if other has a name. Otherwise, the constructed locale has a name if and only if other and one both have names.

Contents

[edit] Parameters

other - another locale to copy
std_name - name of the system locale to use
f - pointer to a facet to merge with other
cats - the facet categories used to identify the facets to merge with other
one - another locale to take facets from

[edit] Exceptions

3,5) std::runtime_error if the operating system has no locale named std_name or if std_name is a null pointer.
4,6) std::runtime_error if the operating system has no locale named std_name.

[edit] Notes

Overload (7) is typically called with its second argument, f, obtained directly from a new-expression: the locale is responsible for calling the matching delete from its own destructor.

[edit] Example

#include <codecvt>
#include <iostream>
#include <locale>
 
std::ostream& operator<< (std::ostream& os, const std::locale& loc)
{
    if (loc.name().length() <= 80)
        os << loc.name();
    else
        for (const auto c : loc.name())
            os << c << (c == ';' ? "\n  " : "");
 
    return os << '\n';
}
 
int main()
{
    std::locale l1;
    std::cout << "Name of a copy of the classic \"C\" locale: " << l1;
 
    std::locale l2("en_US.UTF-8");
    std::cout << "Name of unicode locale: " << l2;
 
    std::locale l3(l1, new std::codecvt_utf8<wchar_t>);
    std::cout << "Name of \"C\" locale except for codecvt: " << l3;
 
    std::locale l4(l1, l2, std::locale::ctype);
    std::cout << "Name of \"C\" locale except for ctype, which is unicode:\n  " << l4;
}

Possible output:

Name of a copy of the classic "C" locale: C
Name of unicode locale: en_US.UTF-8
Name of "C" locale except for codecvt: *
Name of "C" locale except for ctype, which is unicode:
  LC_CTYPE=en_US.UTF-8;
  LC_NUMERIC=C;
  LC_TIME=C;
  LC_COLLATE=C;
  LC_MONETARY=C;
  LC_MESSAGES=C;
  LC_PAPER=C;
  LC_NAME=C;
  LC_ADDRESS=C;
  LC_TELEPHONE=C;
  LC_MEASUREMENT=C;
  LC_IDENTIFICATION=C

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 436 C++98 for overload (7), it was unclear
whether Facet can be cv-qualified
it can be const-qualified,
but not volatile-qualified
LWG 2295 C++98 for overload (7), the constructed locale had no name even if f is null it has the name of other

[edit] See also

destructs the locale and the facets whose reference count becomes zero
(public member function) [edit]

[edit] External links

1.  List of Windows locale names.
2.  List of Linux locale names.