Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | locale
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (Shorten template names. Use {{lc}} where appropriate.)
Line 10: Line 10:
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | loc | the locale object to query }}
+
{{par | loc | the locale object to query }}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
  
Returns a reference the facet. The reference returned by this function is valid as long as any {{c|std::locale}} object exists that implements {{tt|Facet}}.
+
Returns a reference the facet. The reference returned by this function is valid as long as any {{lc|std::locale}} object exists that implements {{tt|Facet}}.
  
 
===Exceptions===
 
===Exceptions===
{{c|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}}.
  
 
===Example===
 
===Example===
Line 39: Line 39:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/locale/dcl list locale}}
+
{{dsc inc | cpp/locale/dcl list locale}}
{{dcl list template | cpp/locale/dcl list has_facet}}
+
{{dsc inc | cpp/locale/dcl list has_facet}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/locale/use facet]]
 
[[de:cpp/locale/use facet]]

Revision as of 19:12, 31 May 2013

 
 
 
Defined in header <locale>
template< class Facet >
const Facet& use_facet( const std::locale& loc );

Obtains a reference to a facet implemented by loc.

Contents

Parameters

loc - the locale object to query

Return value

Returns a reference the facet. The reference returned by this function is valid as long as any std::locale object exists that implements Facet.

Exceptions

std::bad_cast if std::has_facet<Facet>(loc) == false.

Example

Display the 3-letter currency name used by the user's preferred locale

#include <iostream>
#include <locale>
 
int main()
{
    std::locale loc = std::locale(""); // user's preferred locale
    std::cout << "Your currency string is "
              << std::use_facet<std::moneypunct<char, true>>(loc).curr_symbol() << '\n';
}

Output:

Your currency string is USD

See also

Template:cpp/locale/dcl list localeTemplate:cpp/locale/dcl list has facet