Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::use_facet

      From cppreference.com
      <cpp‎ |locale
       
       
       
      Localization library
       
      Defined in header<locale>
      template<class Facet>
      const Facet& use_facet(conststd::locale& loc);

      Obtains a reference to a facet implemented byloc.

      The program is ill-formed if Facet is not afacet whose definition contains the public static memberid 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 anystd::locale object refers to that facet.

      [edit]Exceptions

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

      [edit]Notes

      Astd::locale object should not be a temporary if a reference to theFacet object obtained fromuse_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 objectauto& 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(constchar* 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 USDYour currency string is EURYour currency string is GBP

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 31C++98the 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 38C++98Facet was not required to have a direct memberidrequired
      LWG 436C++98it was unclear whetherFacet can be cv-qualifiedit can be const-qualified, but not volatile-qualified

      [edit]See also

      set of polymorphic facets that encapsulate cultural differences
      (class)[edit]
      checks if a locale implements a specific facet
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/use_facet&oldid=169771"

      [8]ページ先頭

      ©2009-2025 Movatter.jp