Search code examples
c++locale

Are the methods of derived class facets called from the base class?


Say I have a setup like this:

std::locale::global(std::locale("en_US.UTF8"));
stream.imbue(std::locale());

When I attempt to call a method of an instance of a facet using std::use_facet, will its derived class facet's (i.e a "byname" facet) method actually be called? For example, does this:

std::use_facet<std::ctype<char>>(stream.getloc()).is( /*... */ );

do something conceptually equivalent to this:

std::ctype_byname<char>("C").is( /* ... */ );

I figure this is the case because some derived class facets (in this case std::ctype_byname) implement the virtual functions of the base class. Am I right to assume this?


Solution

  • No, it will be a reference to a std::ctype<char> facet that will be returned. The methods of a std::ctype facet (that was not default-constructed on construction of the its locale) will operate based on the locale to which it was installed. Using a byname facet means that you want a specific cultural convention on which to apply its methods that could possibly differ from its locale. It fills the same slot as a base class facet, so use_facet won't return a byname facet.