https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113099
--- Comment #15 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Jonathan Wakely from comment #8) > (In reply to Peter Dimov from comment #7) > > You don't necessarily need dynamic_cast because facets are always installed > > and obtained by their exact type, not via a reference to base. > > Is that true? std::use_facet<X> will return a reference to a facet with > X::id but it could actually be something derived from X. e.g. a user can > install their own facet derived from std::ctype<char>, which overrides some > members. Code that does std::use_facet<std::ctype<char>>(loc) will get the > user's facet, but via reference to base. And conversely, for std::use_facet<Y> the locale might contain a base class of Y, with the same id as Y::id, but there is no Y present. For example, given the OP's my_codecvt, the following code must throw std::bad_cast: std::locale loc; (void) std::use_facet<my_codecvt>(loc); There is no my_codecvt present. There is a facet with the same id, but its dynamic type is std::codecvt<wchar_t, char, mbstate_t> not my_codecvt. I don't see a way to avoid using dynamic_cast without an ABI change to the std::locale in libstdc++.