c++language-lawyerdirectivenon-latin

Are non-Latin characters allowed in #error directive?


Is using non-Latin characters in #error directive allowed by C++ Standard?

E.g. I would like to write an error message in Russian:

#error Сообщение об ошибке
int main() { }

Solution

  • Whether or not you can put non-ASCII characters in an #error directive's argument is "locale-specific" according to C2011 5.2.1p1. The tokens on the line after #error contain characters that are not part of the basic source character set; whether or not they are valid as part of the extended source character set is locale-specific. Per annex J.4, locale-specific behavior is required to be documented, just like implementation-defined behavior.

    The difference between locale-specific and implementation-defined behavior is that there might be several locales, each with its own set of extended source characters. Perhaps only some of those extended source character sets include Cyrillic. These aspects of the C standard were last revised in 1999, before Unicode took over the world, so they're worrying about scenarios such as feeding a source file encoded in ISO 8859-5 to a compiler that expects extended source characters to conform to EUC-JP.

    Regardless of how you actually encode your source files and whether that matches what the compiler expects, what you're trying to do is more likely to work if you use a string literal as the argument of #error:

    #error "Сообщение об ошибке"
    

    This is because some compilers allow a broader variety of characters in string literals than they do in identifiers.