The ctype functions from <locale> throw out std :: bad_cast
This program
#include <iostream>
#include <locale>
int main () {
std::isxdigit(std::cin.peek(), std::cin.getloc());
}
throws a type exception std::bad_castfor me when compiling with gcc or clang using libstdc ++. It works fine with VS2010.
I understand what is going on here. peek()returns intto accommodate the out-of-band EOF value. Locals are not required to have a face ctype<int>(they have this face in VS, possibly as an extension). If the language standard has no facet for the function, it will throw bad_cast.
But shouldn't this work in keeping with the spirit of the original <ctype.h>? Is this a defect in the standard? Is there a generally accepted workaround? I know that I can check the EOF myself and apply it to the appropriate character type, but I would not reinvent the wheel.
No: the character type must be known (suppose a 32-bit int is a character type having a 64-bit representation for EOF). It cannot be resolved, the locale is not limited to a certain type of character, but these are faces.
Availability:
std::isxdigit<char>(std::cin.peek(), std::cin.getloc());
clarify the call and (!) ignore EOF, making it char (int (-1)).
Therefore, you can check the EOF yourself.