简体   繁体   中英

Are non-Latin characters allowed in #error directive?

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

Eg I would like to write an error message in Russian:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM