变量名中的美元符号

发布于 2024-12-12 08:45:13 字数 337 浏览 0 评论 0 原文

我偶然发现了一些这样的 C++ 代码:

int $T$S;

首先我认为这是某种 PHP 代码或者错误地粘贴在那里,但它编译并运行得很好(在 MSVC 2008 年)。

什么样的字符对 C++ 中的变量有效?还有其他奇怪的字符可以使用吗?

I stumble upon some C++ code like this:

int $T$S;

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

What kind of characters are valid for variables in C++ and are there other weird characters you can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一直都在从未离去 2024-12-19 08:45:14

根据标准,唯一合法的字符是字母数字
和下划线。该标准确实要求任何事情
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 '_'.

霞映澄塘 2024-12-19 08:45:14

它是某些编译器的扩展,而不是 C 标准

MSVC 中的扩展:

微软特定

只有 Microsoft C++ 标识符的前 2048 个字符是重要的。用户定义类型的名称由编译器“修饰”以保留类型信息。结果名称(包括类型信息)不能超过 2048 个字符。 (有关详细信息,请参阅修饰名称。)可能影响修饰标识符长度的因素有:

  • 标识符是否表示用户定义类型的对象或从用户定义类型派生的类型。
  • 标识符是否表示函数或从函数派生的类型。
  • 函数的参数数量。

美元符号也是 Visual C++ 中的有效标识符。

//dollar_sign_identifier.cpp
结构$Y1$ {
    无效$测试$(){}
};

int main() {
    $Y1$x$;
    $x$.$测试$();
}

https://web.archive.org/web/20100216114436/http://msdn.microsoft.com/en-us/library/565w213d.aspx

最新版本:https://learn.microsoft.com /en-us/cpp/cpp/identifiers-cpp?redirectedfrom=MSDN&view=vs-2019

GCC:

6.42 标识符名称中的美元符号

在 GNU C 中,通常可以在标识符名称中使用美元符号。这是因为许多传统的 C 实现允许此类标识符。然而,一些目标机器不支持标识符中的美元符号,通常是因为目标汇编器不允许它们。

http://gcc.gnu.org/onlinedocs/gcc/Dollar -Signs.html#Dollar-Signs

It's an extension of some compilers and not in the C standard

MSVC:

Microsoft Specific

Only the first 2048 characters of Microsoft C++ identifiers are significant. Names for user-defined types are "decorated" by the compiler to preserve type information. The resultant name, including the type information, cannot be longer than 2048 characters. (See Decorated Names for more information.) Factors that can influence the length of a decorated identifier are:

  • Whether the identifier denotes an object of user-defined type or a type derived from a user-defined type.
  • Whether the identifier denotes a function or a type derived from a function.
  • The number of arguments to a function.

The dollar sign is also a valid identifier in Visual C++.

// dollar_sign_identifier.cpp
struct $Y1$ {
    void $Test$() {}
};

int main() {
    $Y1$ $x$;
    $x$.$Test$();
}

https://web.archive.org/web/20100216114436/http://msdn.microsoft.com/en-us/library/565w213d.aspx

Newest version: https://learn.microsoft.com/en-us/cpp/cpp/identifiers-cpp?redirectedfrom=MSDN&view=vs-2019

GCC:

6.42 Dollar Signs in Identifier Names

In GNU C, you may normally use dollar signs in identifier names. This is because many traditional C implementations allow such identifiers. However, dollar signs in identifiers are not supported on a few target machines, typically because the target assembler does not allow them.

http://gcc.gnu.org/onlinedocs/gcc/Dollar-Signs.html#Dollar-Signs

南渊 2024-12-19 08:45:14

据我所知,只有字母(大写和小写)、数字09)和根据标准,_ 对变量名称有效(注意:变量名称不应以数字开头)。

所有其他字符都应该是编译器扩展。

To my knowledge, only letters (capital and small), numbers (0 to 9) and _ are valid for variable names according to the standard (note: the variable name should not start with a number though).

All other characters should be compiler extensions.

定格我的天空 2024-12-19 08:45:14

这不是一个好的做法。通常,您应该仅在标识符中使用字母数字字符和下划线 ([az][AZ][0-9]_)。

表面层

与其他语言(bash、perl)不同,C 不使用 $ 来表示变量的使用。因此,它在技术上是有效的。在 C 语言中,它很可能属于 C11,6.4.2。这意味着现代编译器似乎确实支持它。

至于你的C++问题,让我们测试一下!

int main(void) {
    int $ = 0;
    return $;
}

在 GCC/G++/Clang/Clang++ 上,这确实可以编译并运行得很好。

更深层次的

编译器获取源代码,将其词法到令牌流中,将其放入抽象语法树(AST)中,然后使用它来生成代码(例如汇编/LLVM IR)。你的问题实际上只围绕第一部分(例如词法分析)。

C/C++ 的语法(因此是词法分析器实现)不会将 $ 视为特殊的,与逗号、句点、细箭头等不同......因此,您可能会从词法分析器获得如下输出:来自下面的 C 代码:

int i_love_$ = 0;

在词法分析器之后,这变成了一个令牌流,如下所示:

["int", "i_love_$", "=", "0"]

如果您在哪里获取此代码:

int i_love_$,_and_.s = 0;

词法分析器将输出一个令牌流,如下所示:

["int", "i_love_$", ",", "_and_", ".", "s", "=", "0"]

如您所见,因为 C/C++ 不处理字符就像 $ 一样特别,它处理方式与其他字符(如句点)不同。

This is not good practice. Generally, you should only use alphanumeric characters and underscores in identifiers ([a-z][A-Z][0-9]_).

Surface Level

Unlike in other languages (bash, perl), C does not use $ to denote the usage of a variable. As such, it is technically valid. In C it most likely falls under C11, 6.4.2. This means that it does seem to be supported by modern compilers.

As for your C++ question, lets test it!

int main(void) {
    int $ = 0;
    return $;
}

On GCC/G++/Clang/Clang++, this indeed compiles, and runs just fine.

Deeper Level

Compilers take source code, lex it into a token stream, put that into an abstract syntax tree (AST), and then use that to generate code (e.g. assembly/LLVM IR). Your question really only revolves around the first part (e.g. lexing).

The grammar (thus the lexer implementation) of C/C++ does not treat $ as special, unlike commas, periods, skinny arrows, etc... As such, you may get an output from the lexer like this from the below c code:

int i_love_$ = 0;

After the lexer, this becomes a token steam like such:

["int", "i_love_
quot;, "=", "0"]

If you where to take this code:

int i_love_$,_and_.s = 0;

The lexer would output a token steam like:

["int", "i_love_
quot;, ",", "_and_", ".", "s", "=", "0"]

As you can see, because C/C++ doesn't treat characters like $ as special, it is processed differently than other characters like periods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文