Search code examples
c++non-ascii-characters

Why does std::iswalpha return false for some French characters in C++?


I am using std::iswalpha to check if a non-ASCII character (French character) is alphabetic. However, I have found that it returns false for é character. I have set my locale to fr_FR.UTF-8 in my code. Can you help me understand why this is happening and how I can correctly determine whether a French character is alphabetic using C++?

#include <iostream>
#include <cwctype>

int main() {
    std::setlocale(LC_ALL, "fr_FR.UTF-8");
    wchar_t ch = L'é';
    bool is_alpha = std::iswalpha(ch);
    std::cout << is_alpha << std::endl; // print 0
    return 0;
}

Solution

  • It's because installing the locale fails, but you don't check that.

    Running this in Compiler Explorer prints locale: No such file or directory while running it on my local computer where I have the fr_FR.UTF-8 locale installed succeeds and prints 1:

    #include <cstdio>
    #include <cwctype>
    #include <iostream>
    
    int main() {
        if (std::setlocale(LC_ALL, "fr_FR.UTF-8") == nullptr) {
            std::perror("locale");
        } else {
            wint_t ch = L'é';
            bool is_alpha = std::iswalpha(ch);
            std::cout << is_alpha << std::endl;  // print 1
        }
    }