Difference between revisions of "cpp/locale/use facet"
From cppreference.com
m (Minor tweak.) |
m (→Notes: +) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 7: | Line 7: | ||
}} | }} | ||
− | Obtains a reference to a facet implemented by {{c|loc}}. | + | Obtains a reference to a facet implemented by {{c|loc}}. |
+ | |||
+ | The program is ill-formed if Facet is not a {{rlp|locale/facet#Facet class|facet}} whose definition contains the public static member {{ttb|id}} or it is a volatile-qualified facet. | ||
===Parameters=== | ===Parameters=== | ||
Line 19: | Line 21: | ||
===Exceptions=== | ===Exceptions=== | ||
{{lc|std::bad_cast}} if {{c|1=std::has_facet<Facet>(loc) == false}}. | {{lc|std::bad_cast}} if {{c|1=std::has_facet<Facet>(loc) == false}}. | ||
+ | |||
+ | ===Notes=== | ||
+ | A {{lc|std::locale}} object should not be a temporary if a reference to the {{tt|Facet}} object obtained from {{tt|use_facet}} is used after the end of statement: | ||
+ | {{source|1= | ||
+ | // BAD: | ||
+ | auto& f = std::use_facet<std::moneypunct<char, true>>(std::locale{"no_NO.UTF-8"}); | ||
+ | foo(f.curr_symbol()); // Error: f internally uses a dangling reference | ||
+ | // to a std::locale object that no longer exists. | ||
+ | // GOOD: | ||
+ | auto loc = std::locale{"is_IS.UTF-8"}; // OK: a non-temporary object | ||
+ | auto& f = std::use_facet<std::moneypunct<char, true>>(loc); | ||
+ | foo(f.curr_symbol()); // OK: f internally uses a reference to existing locale object. | ||
+ | }} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | |Display the 3-letter currency name used by the user's preferred locale | + | |Display the 3-letter currency name used by the user's preferred locale. |
|code= | |code= | ||
#include <iostream> | #include <iostream> | ||
Line 29: | Line 44: | ||
int main() | int main() | ||
{ | { | ||
− | + | for (const char* name: {"en_US.UTF-8", "de_DE.UTF-8", "en_GB.UTF-8"}) | |
− | + | std::cout << "Your currency string is " | |
− | + | << std::use_facet<std::moneypunct<char, true>>(std::locale{name}). | |
+ | curr_symbol() << '\n'; | ||
} | } | ||
|output= | |output= | ||
Your currency string is USD | Your currency string is USD | ||
+ | Your currency string is EUR | ||
+ | Your currency string is GBP | ||
}} | }} | ||
Line 40: | Line 58: | ||
{{dr list begin}} | {{dr list begin}} | ||
{{dr list item|wg=lwg|dr=31|std=C++98|before=the returned reference remained usable<br>as long as the locale value itself exists|after=the returned reference remains usable as<br>long as some locale object refers to that facet}} | {{dr list item|wg=lwg|dr=31|std=C++98|before=the returned reference remained usable<br>as long as the locale value itself exists|after=the returned reference remains usable as<br>long as some locale object refers to that facet}} | ||
+ | {{dr list item|wg=lwg|dr=38|std=C++98|before={{tt|Facet}} was not required to have a direct member {{ttb|id}}|after=required}} | ||
+ | {{dr list item|wg=lwg|dr=436|std=C++98|before=it was unclear whether {{tt|Facet}} can be cv-qualified|after=it can be const-qualified, but not volatile-qualified}} | ||
{{dr list end}} | {{dr list end}} | ||
Latest revision as of 14:21, 12 February 2024
Defined in header <locale>
|
||
template< class Facet > const Facet& use_facet( const std::locale& loc ); |
||
Obtains a reference to a facet implemented by loc.
The program is ill-formed if Facet is not a facet whose definition contains the public static member id
or it is a volatile-qualified facet.
Contents |
[edit] Parameters
loc | - | the locale object to query |
[edit] Return value
Returns a reference to the facet. The reference returned by this function is valid as long as any std::locale object refers to that facet.
[edit] Exceptions
std::bad_cast if std::has_facet<Facet>(loc) == false.
[edit] Notes
A std::locale object should not be a temporary if a reference to the Facet
object obtained from use_facet
is used after the end of statement:
// BAD: auto& f = std::use_facet<std::moneypunct<char, true>>(std::locale{"no_NO.UTF-8"}); foo(f.curr_symbol()); // Error: f internally uses a dangling reference // to a std::locale object that no longer exists. // GOOD: auto loc = std::locale{"is_IS.UTF-8"}; // OK: a non-temporary object auto& f = std::use_facet<std::moneypunct<char, true>>(loc); foo(f.curr_symbol()); // OK: f internally uses a reference to existing locale object.
[edit] Example
Display the 3-letter currency name used by the user's preferred locale.
Run this code
#include <iostream> #include <locale> int main() { for (const char* name: {"en_US.UTF-8", "de_DE.UTF-8", "en_GB.UTF-8"}) std::cout << "Your currency string is " << std::use_facet<std::moneypunct<char, true>>(std::locale{name}). curr_symbol() << '\n'; }
Output:
Your currency string is USD Your currency string is EUR Your currency string is GBP
[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 31 | C++98 | the returned reference remained usable as long as the locale value itself exists |
the returned reference remains usable as long as some locale object refers to that facet |
LWG 38 | C++98 | Facet was not required to have a direct member id
|
required |
LWG 436 | C++98 | it was unclear whether Facet can be cv-qualified
|
it can be const-qualified, but not volatile-qualified |
[edit] See also
set of polymorphic facets that encapsulate cultural differences (class) | |
checks if a locale implements a specific facet (function template) |