在 Visual Studio 或 GCC 的 C++ 中使用表情符号作为标识符名称
Using emoji as identifier names in c++ in Visual Studio or GCC
传统上,我们可以在 C++ 中用作标识符一部分的可接受字符是第一个字符后的 _, a-z, A-Z
和 0-9
。
有没有办法配置 Visual Studio 或 GCC 以接受表情符号作为标识符名称(或任何其他任意 unicode 字符)的一部分?
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);
从Unicode/special characters in variable names in clang not allowed? that the C++ standard allows certain sets of extended characters. The emoji codes可以看出似乎落在了允许的范围内。
据我所知使用这个 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.
并提供 link 表明自 2005 年以来允许这样做。尽管正如 bames53 指出的那样,表情符号可能存在 Windows 限制。
另一方面,gcc
似乎不支持这一点,除非使用转义码,来自他们的 Character sets 文档:
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.
从 gcc 10 开始,gcc 现在接受表情符号作为标识符名称的一部分。
传统上,我们可以在 C++ 中用作标识符一部分的可接受字符是第一个字符后的 _, a-z, A-Z
和 0-9
。
有没有办法配置 Visual Studio 或 GCC 以接受表情符号作为标识符名称(或任何其他任意 unicode 字符)的一部分?
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);
从Unicode/special characters in variable names in clang not allowed? that the C++ standard allows certain sets of extended characters. The emoji codes可以看出似乎落在了允许的范围内。
据我所知使用这个 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.
并提供 link 表明自 2005 年以来允许这样做。尽管正如 bames53 指出的那样,表情符号可能存在 Windows 限制。
另一方面,gcc
似乎不支持这一点,除非使用转义码,来自他们的 Character sets 文档:
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.
从 gcc 10 开始,gcc 现在接受表情符号作为标识符名称的一部分。