問題描述
我偶然發(fā)現(xiàn)了一些這樣的 C++ 代碼:
I stumbled on some C++ code like this:
int $T$S;
首先,我認(rèn)為這是某種 PHP 代碼或錯(cuò)誤粘貼的內(nèi)容,但它編譯并運(yùn)行良好(在 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).
哪些字符對(duì) C++ 中的變量有效?還有其他奇怪的字符可以使用嗎?
What kind of characters are valid for variables in C++ and are there any other weird characters you can use?
推薦答案
唯一符合標(biāo)準(zhǔn)的合法字符是字母數(shù)字和下劃線.該標(biāo)準(zhǔn)確實(shí)要求幾乎任何事情Unicode 認(rèn)為字母是可以接受的(但只能作為單個(gè)代碼點(diǎn)字符).在實(shí)踐中,實(shí)現(xiàn)提供了擴(kuò)展(即有些確實(shí)接受 $)和限制(大多數(shù)不接受所有所需的 Unicode 字符).如果你希望你的代碼是可移植的,將符號(hào)限制為 26 個(gè)無重音字母,大寫或小寫,十位數(shù)字和_".
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 '_'.
這篇關(guān)于變量名中的美元符號(hào)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!