Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using emoji as identifier names in c++ in Visual Studio or GCC

Tags:

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); 
like image 793
ButterDog Avatar asked May 08 '15 18:05

ButterDog


People also ask

Can you use emoji in variable names?

Users can not only use an emoji as a variable name but it can also be used as an alias in the import statement.

Are Emojis Ascii or Unicode?

Because emoji characters are treated as pictographs, they are encoded in Unicode based primarily on their general appearance, not on an intended semantic.

Is Emoji a character set?

What are Emojis? Emojis look like images, or icons, but they are not. They are letters (characters) from the UTF-8 (Unicode) character set.

Can Unicode represent Emojis?

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.


2 Answers

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.

like image 79
Shafik Yaghmour Avatar answered Sep 29 '22 15:09

Shafik Yaghmour


Since gcc 10, gcc now accepts emoji as part of the identifier names.

Source: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67224

like image 22
Flair Avatar answered Sep 29 '22 16:09

Flair