25

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ButterDog
  • 5,115
  • 6
  • 43
  • 61
  • 7
    Closely related [Unicode/special characters in variable names in clang not allowed?](http://stackoverflow.com/q/26660180/1708801) ... [it works in clang](http://melpon.org/wandbox/permlink/CXG1mPgi9obikNBz) – Shafik Yaghmour May 08 '15 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. – bames53 May 08 '15 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. – bames53 May 08 '15 at 19:37
  • 1
    clang is the one with better support for extended characters, but only in variable names, no in functions for some reason. – alfC Feb 27 '17 at 03:10
  • 2
    [ (and other unicode characters) in identifiers not allowed by g++](http://stackoverflow.com/q/12692067/995714) – phuclv Apr 30 '17 at 10:27
  • 2
    The horror, the horror – Carbon Oct 31 '17 at 20:38
  • You might have some luck displaying emoji in Windows if you `chcp 65001` to put your terminal in UTF-8 mode and select a font in your console/editor that contains them, such as DejaVu Sans Mono. This doesn't give you color emoji. – Davislor Sep 04 '18 at 07:43

2 Answers2

9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Flair
  • 2,609
  • 1
  • 29
  • 41