(and other unicode characters) in identifiers not allowed by g++
As of 4.8, gcc does not support characters outside of the BMP used as identifiers. It seems to be an unnecessary restriction. Also, gcc only supports a very restricted set of character described in ucnid.tab, based on C99 and C++98 (it is not updated to C11 and C++11 yet, it seems).
As described in the manual, -fextended-identifiers
is experimental, so it has a higher chance won't work as expected.
Edit:
GCC supported the C11 character set starting from 4.9.0 (svn r204886 to be precise). So OP's second piece of code using \U0001F603
does work. I still can't get the actual code using ð
to work even with -finput-charset=UTF-8
with GCC 8.2 on https://gcc.godbolt.org though (You may want to follow this bug report, provided by @DanielWolf).
Meanwhile both pieces of code work on clang 3.3 without any options other than -std=c++11
.
However, the standard specifically allows characters from the range 10000-1FFFD in Annex E.1 and doesn't disallow it as an initial character in E.2.
One thing to keep in mind is that just because the C++ standard allows (or disallows) some feature, does not necessarily mean that your compiler supports (or doesn't support) that feature.
This was a known bug in GCC 9 and before. This has been fixed in GCC 10.
The official changelog for GCC 10 contains this section:
Extended characters in identifiers may now be specified directly in the input encoding (UTF-8, by default), in addition to the UCN syntax (
\uNNNN
or\UNNNNNNNN
) that is already supported:
static const int π = 3;
int get_naïve_pi() {
return π;
}