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

陌路散爱 提交于 2019-12-18 05:41:36

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/30130806/using-emoji-as-identifier-names-in-c-in-visual-studio-or-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!