函数声明和签名有什么区别?

发布于 2024-08-22 16:10:11 字数 107 浏览 7 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(5

趴在窗边数星星i 2024-08-29 16:10:11

函数声明是函数的原型(或者,如果编译器当时没有看到原型,则它可以来自函数定义) - 它包括返回类型、函数名称和参数类型(可选地在C)中。

函数签名是编译器用来执行重载决策的函数声明的一部分。由于多个函数可能具有相同的名称(即,它们被重载),因此编译器需要一种方法来确定函数调用应解析为具有特定名称的几个可能函数中的哪一个。签名是编译器在重载决策中考虑的内容。具体来说,该标准将“签名”定义为:

有关参与重载决策的函数的信息:其参数的类型,如果该函数是类成员,则函数本身的 cv 限定符(如果有)以及成员函数所在的类声明。

请注意,返回类型不是函数签名的一部分。正如标准在脚注中所说,“函数签名不包括返回类型,因为它不参与重载解析”。

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:

the information about a function that participates in overload resolution: the types of its parameters and, if the function is a class member, the cv-qualifiers (if any) on the function itself and the class in which the member function is declared.

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

无声情话 2024-08-29 16:10:11

该标准定义了两个术语:声明和定义。定义是一个暂定的声明。然而,C99 和 C++03 标准的定义略有不同。

来自 C++0x 草案:

附录C

8.3.5 更改:在 C++ 中,声明的函数
参数列表为空时则不需要
论据。在C语言中,空参数
list 表示的数量和类型
函数参数未知”

定义

1.3.11签名

名称和参数类型列表
函数的 (8.3.5) 以及
类、概念、概念图或
它所属的命名空间。如果
函数或函数模板是
类成员其签名
另外还包括
简历限定符(如果有)和
函数上的引用限定符(如果有)
或函数模板本身。这
受限制成员的签名
(9.2) 包括其模板
要求。的签名
另外函数模板
包括它的返回类型、它的模板
参数列表及其模板
要求(如果有)。签名
函数模板特化
包括模板的签名
它是其中的一个专业,并且
它的模板参数(是否
明确指定或推论)。 [
注:签名作为依据
用于名称修改和链接。—结束
注]

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:

Appendix C

8.3.5 Change: In C++, a function declared
with an empty parameter list takes no
arguments. In C, an empty parameter
list means that the number and type of
the function arguments are unknown"

Definitions

1.3.11 signature

the name and the parameter-type-list
(8.3.5) of a function, as well as the
class, concept, concept map, or
namespace of which it is a member. If
a function or function template is a
class member its signature
additionally includes the
cv-qualifiers (if any) and the
ref-qualifier (if any) on the function
or function template itself. The
signature of a constrained member
(9.2) includes its template
requirements. The signature of a
function template additionally
includes its return type, its template
parameter list, and its template
requirements (if any). The signature
of a function template specialization
includes the signature of the template
of which it is a specialization and
its template arguments (whether
explicitly specified or deduced). [
Note: Signatures are used as a basis
for name mangling and linking.—end
note ]

怪我鬧 2024-08-29 16:10:11

函数签名不包括函数的返回类型或链接类型。

好的,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?

巡山小妖精 2024-08-29 16:10:11

另请注意,根据标准,参数上的顶级 const 和 volatile 不是签名的一部分。但有些编译器会犯这个错误。

例如

void f(const int, const char* const);

具有相同的签名

void f(int, const char*);

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.

void f(const int, const char* const);

has the same signature as

void f(int, const char*);
调妓 2024-08-29 16:10:11

函数声明是一个原型。函数签名指示返回类型以及构成签名的参数。考虑一下:

int foo(int, int);  /* Function Declaration */


/* Implementation of foo 
** Function signature
*/
int foo(int a, int b){
}

现在,考虑一下这种情况:程序员被问到 foo 的函数签名是什么:

  • 它返回 int 数据类型
  • 两个参数也是 数据类型>int,分别命名为 ab

另一方面,函数原型是在 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:

int foo(int, int);  /* Function Declaration */


/* Implementation of foo 
** Function signature
*/
int foo(int a, int b){
}

Now, consider this scenario: a programmer is asked what is the function signature for foo:

  • It returns a datatype of int
  • Two parameters are also of datatype of int, named a and b respectively

The 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'.

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