ButterDog
ButterDog

Reputation: 5225

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

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);

Upvotes: 26

Views: 19082

Answers (2)

Flair
Flair

Reputation: 2907

Since GCC 10 (2020-05-07), GCC now accepts emoji as part of the identifier names.

Source: Bug 67224. Summary: UTF-8 support for identifier names in GCC

Upvotes: 4

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158499

We can see from Are Unicode and 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 it provides a link which indicates this was allowed since 2005. Although, as bames53 points out, there may be Windows limitations with respect to 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.

Upvotes: 10

Related Questions