函数声明和签名有什么区别?
在 C 或 C++ 中,函数声明和函数签名有什么区别?
我知道一些函数声明,但函数签名对我来说是全新的。函数签名这个概念有什么意义呢?这两个概念实际上有什么用?
谢谢!
In C or C++ what is the difference between function declaration and function signature?
I know something of function declaration but function signature is totally new to me. What is the point of having the concept of function signature? What are the two concepts used for actually?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
函数声明是函数的原型(或者,如果编译器当时没有看到原型,则它可以来自函数定义) - 它包括返回类型、函数名称和参数类型(可选地在C)中。
函数签名是编译器用来执行重载决策的函数声明的一部分。由于多个函数可能具有相同的名称(即,它们被重载),因此编译器需要一种方法来确定函数调用应解析为具有特定名称的几个可能函数中的哪一个。签名是编译器在重载决策中考虑的内容。具体来说,该标准将“签名”定义为:
请注意,返回类型不是函数签名的一部分。正如标准在脚注中所说,“函数签名不包括返回类型,因为它不参与重载解析”。
A function declaration is the prototype for a function (or it can come from the function definition if no prototype has been seen by the compiler at that point) - it includes the return type, the name of the function and the types of the parameters (optionally in C).
A function signature is the parts of the function declaration that the compiler uses to perform overload resolution. Since multiple functions might have the same name (ie., they're overloaded), the compiler needs a way to determine which of several possible functions with a particular name a function call should resolve to. The signature is what the compiler considers in that overload resolution. Specifically, the standard defines 'signature' as:
Note that the return type is not part of the function signature. As the standard says in a footnote, "Function signatures do not include return type, because that does not participate in overload resolution".
该标准定义了两个术语:声明和定义。定义是一个暂定的声明。然而,C99 和 C++03 标准的定义略有不同。
来自 C++0x 草案:
The standard defines two terms: declaration and definition. A definition is a tentative declaration. However, the C99 and C++03 standards have slightly varying definitions.
From C++0x draft:
函数签名不包括函数的返回类型或链接类型。
好的,Wikipedia 不同意我关于包含返回类型的观点。但是我知道编译器在确定函数调用是否与签名匹配时不使用返回类型。之前的 StackOverflow 问题似乎同意: Is the return type part of函数签名?
The function signature doesn't include the return type or linkage type of the function.
OK, Wikipedia disagrees with me on the return type being included. However I know that the return type is not used by the compiler when deciding if a function call matches the signature. This previous StackOverflow question appears to agree: Is the return type part of the function signature?
另请注意,根据标准,参数上的顶级 const 和 volatile 不是签名的一部分。但有些编译器会犯这个错误。
例如
具有相同的签名
Also please note that top-level const and volatile on argument are not part of the signature, according to the standard. But some compilers get this wrong.
e.g.
has the same signature as
函数声明是一个原型。函数签名指示返回类型以及构成签名的参数。考虑一下:
现在,考虑一下这种情况:程序员被问到
foo
的函数签名是什么:int
数据类型数据类型>int
,分别命名为a
和b
另一方面,函数原型是在 C/C++ 编译器中提供线索,了解预期的内容以及是否签名与原型不匹配,编译器将沿着“函数声明错误”或“原型不匹配”的上下文发出错误。
A function declaration is a prototype. A function signature indicates what is the return type and the parameters used that makes up the signature. Consider this:
Now, consider this scenario: a programmer is asked what is the function signature for
foo
:int
int
, nameda
andb
respectivelyThe function prototype on the other hand is to clue in the C/C++ compiler, on what to expect and if the signature does not match up with the prototype, the compiler will emit an error, along the context of 'function declaration error' or 'prototype mismatch'.