26

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); 
7
  • 7
    Closely related Unicode/special characters in variable names in clang not allowed? ... it works in clang Commented May 8, 2015 at 18:47
  • 5
    The answer is that the C++ spec since C++11 requires compilers to accept emoji characters in identifiers and that yes, some mainstream compilers accept them. Commented May 8, 2015 at 19:37
  • 2
    Clang has good support, and on OS X there's good support for displaying emoji. MSVC seems to have pretty good support as of at least vs2013, however Window's support for displaying emoji is less than one might desire, at least until 8.1. GCC 5 enables their "-fextended-identifiers" option by default, however this only enables emoji in identifiers in the form of UCNs: UCNs and literally writing the emoji (e.g., using UTF-8) are required by the spec to behave identically, but gcc doesn't yet do this. Commented May 8, 2015 at 19:37
  • 2
    😃 (and other unicode characters) in identifiers not allowed by g++ Commented Apr 30, 2017 at 10:27
  • 2
    The horror, the horror Commented Oct 31, 2017 at 20:38

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.