Traditionally, the accepted characters we can use as part of an identifier in C++ are _, a-z, A-Z
and 0-9
after the first character.
Is there a way to configure Visual Studio or GCC to accept emoji as part of the identifier names (or any other arbitrary unicode character)?
int a = 2, š = 3; š++; š *= 2; int ā(int a, int b) {return a + b;} cout << ā(a * š, 3) << endl; const double Ļ = 3.14159; double Ī± = sin(Ļ / 2.0);
Users can not only use an emoji as a variable name but it can also be used as an alias in the import statement.
Because emoji characters are treated as pictographs, they are encoded in Unicode based primarily on their general appearance, not on an intended semantic.
What are Emojis? Emojis look like images, or icons, but they are not. They are letters (characters) from the UTF-8 (Unicode) character set.
The Unicode Standard has assigned numbers to represent emojis. Here's how it works. In the Unicode Standard, each emoji is represented as a "code point" (a hexadecimal number) that looks like U+1F063, for example.
We can see from Unicode/special characters in variable names in clang not allowed? that the C++ standard allows certain sets of extended characters. The emoji codes seem to fall into the allowed ranges.
As far as I can tell using this live example Visual Studio 2013 supports extended characters in identifiers and this is supported by the C++ Identifiers documentation:
C++ specification allows naming with Unicode-characters
And also, Visual C++ itself allows too. Not ASCII limited.
and provides link which indicates this was allowed since 2005. Although as bames53 points out there may be Windows limitations with respect emoji.
gcc
on the other hand does not seem to support this except by using escape codes, from their Character sets document:
In identifiers, characters outside the ASCII range can only be specified with the ā\uā and ā\Uā escapes, not used directly. If strict ISO C90 conformance is specified with an option such as -std=c90, or -fno-extended-identifiers is used, then those escapes are not permitted in identifiers.
Since gcc 10, gcc now accepts emoji as part of the identifier names.
Source: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67224
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With