Search code examples
c++visual-c++ctype

std::ctype Derived Class Fails to Compile for char


The code below fails to compile with: override did not override any base class methods & do_is is not a member of ctype.

It works fine for wchar_t.

Tested on VC++ 2022, default settings. [EDIT] I got the same result for online GCC. It looks like it is a feature, but why?

#include <locale>

struct fail_t : std::ctype<char> // works for wchar_t
{
  bool do_is(mask m, char_type c) const override
  {
    return ctype::do_is(m, c);
  }
};

int main()
{
  // nop
}

Solution

  • Perhaps not a complete answer, but the cppreference site page for the std::ctype<char> specialization1 does briefly explain (bolding mine):

    This specialization of std::ctype encapsulates character classification features for type char. Unlike general-purpose std::ctype, which uses virtual functions, this specialization uses table lookup to classify characters (which is generally faster).

    Note also, on that page, that there is no do_is() member function (inherited or otherwise).

    As for the "but why" part of your question: I guess the last (parenthesised) phrase covers that: which is generally faster.


    1 I appreciate that cppreference does not represent any official C++ Standard; however, it is generally very reliable and the language used is often rather more understandable than that in those Standard documents.


    Looking through this C++17 Draft Standard, there is another possible answer to the "but why" question:

    25.4.1.3 ctype specializations [facet.ctype.special]

    1     A specialization ctype<char> is provided so that the member functions on type char can be implemented inline.