变量名中的美元符号?
我偶然发现了一些这样的 C++ 代码:
I stumbled on some C++ code like this:
int $T$S;
首先,我认为这是某种 PHP 代码或错误粘贴的内容,但它编译并运行良好(在 MSVC 2008 上).
First I thought that it was some sort of PHP code or something wrongly pasted in there but it compiles and runs nicely (on MSVC 2008).
哪些字符对 C++ 中的变量有效?还有其他奇怪的字符可以使用吗?
What kind of characters are valid for variables in C++ and are there any other weird characters you can use?
推荐答案
唯一符合标准的合法字符是字母数字和下划线.该标准确实要求几乎任何事情Unicode 认为字母是可以接受的(但只能作为单个代码点字符).在实践中,实现提供了扩展(即有些确实接受 $)和限制(大多数不接受所有所需的 Unicode 字符).如果你希望你的代码是可移植的,将符号限制为 26 个无重音字母,大写或小写,十位数字和_".
The only legal characters according to the standard are alphanumerics and the underscore. The standard does require that just about anything Unicode considers alphabetic is acceptable (but only as single code-point characters). In practice, implementations offer extensions (i.e. some do accept a $) and restrictions (most don't accept all of the required Unicode characters). If you want your code to be portable, restrict symbols to the 26 unaccented letters, upper or lower case, the ten digits, and the '_'.
相关文章