std::has_facet(3)
π std::has_facet β the C++ Standard Library glow-up
okay bestie, so this one is from the C++ Standard Library manual set (std::has_facet(3)), and it's genuinely one of those little functions that sounds scary and is actually just a vibe check for locales. let's get into it π«Ά
β¨ NAME β¨
std::has_facet β literally just std::has_facet, no notes, the name is the name π
πΈ SYNOPSIS πΈ
so <locale> is the header it lives in, and it's got two eras, ok? pre-C++11 and post-C++11, and the only glow-up is throw() becoming noexcept:
Defined in header <locale>
template< class Facet > (until C++11)
bool has_facet( const locale& loc ) throw();
template< class Facet > (since C++11)
bool has_facet( const locale& loc ) noexcept;
what it does: checks if the locale loc implements the facet Facet π¦
and bestie, listen to me, this is the important bit: the program is ill-formed if Facet is not a facet, or if it's a volatile-qualified facet. not "oops undefined behaviour" ill-formed, like, ill-formed. no cap. π
π Parameters π
locβ the locale object you're querying, the one you're side-eyeing to see what it's got π
β Return value β
returns true if the facet Facet was installed in the locale loc, and false otherwise. that's it. that's the whole function. slay. β¨
π Notes π
std::has_facet must return true for all locales loc if Facet is one of the standard facets given here (iykyk, check the standard list π
).
π Example π
run this code, it's adorable:
#include <iostream>
#include <locale>
// minimal custom facet
struct myfacet : public std::locale::facet
{
static std::locale::id id;
};
std::locale::id myfacet::id;
int main()
{
// loc is a "C" locale with myfacet added
std::locale loc(std::locale::classic(), new myfacet);
std::cout << std::boolalpha
<< "Can loc classify chars? "
<< std::has_facet<std::ctype<char>>(loc) << '\n'
<< "Can loc classify char32_t? "
<< std::has_facet<std::ctype<char32_t>>(loc) << '\n'
<< "Does loc implement myfacet? "
<< std::has_facet<myfacet>(loc) << '\n';
}
π Output π
Can loc classify chars? true
Can loc classify char32_t? false
Does loc implement myfacet? true
π§ Defect reports π§
so these are behaviour-changing defect reports that got applied retroactively to previously published C++ standards. history really said "let's fix that" π
- DR
LWG 436, applied to C++98 β the behaviour as published was that it was unclear whetherFacetcan be cv-qualified. the correct behaviour: it can be const-qualified, but not volatile-qualified π
π See also π
localeβ set of polymorphic facets that encapsulate cultural differences (class)use_facetβ obtains a facet from a locale (function template)
kk that's the whole page, you ate it, no crumbs π this was served fresh from cppreference.com, dated 2024.06.10 β go be the main character of your own locale, bestie π¦β¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms