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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2322736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:01:54  来源:igfitidea点击:

what is the difference between function declaration and signature?

c++cfunctiondeclarationsignature

提问by Tim

In C or C++ what is the difference between function declaration and function signature?

在 C 或 C++ 中,函数声明和函数签名有什么区别?

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!

谢谢!

回答by Michael Burr

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

函数声明是函数的原型(或者,如果此时编译器没有看到原型,则它可以来自函数定义)——它包括返回类型、函数名称和参数类型(可选在 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.

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

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

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

回答by dirkgently

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.

该标准定义了两个术语:声明和定义。定义是临时声明。但是,C99 和 C++03 标准的定义略有不同。

From C++0x draft:

来自 C++0x 草案:

Appendix C

8.3.5Change: 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 ]

附录 C

8.3.5更改:在 C++ 中,使用空参数列表声明的函数不接受任何参数。在 C 中,空参数列表意味着函数参数的数量和类型是未知的”

定义

1.3.11 签名

函数的名称和参数类型列表 (8.3.5),以及它所属的类、概念、概念图或命名空间。如果函数或函数模板是类成员,则其签名还包括函数或函数模板本身的 cv 限定符(如果有)和 ref 限定符(如果有)。受约束成员 (9.2) 的签名包括其模板要求。函数模板的签名还包括其返回类型、模板参数列表和模板要求(如果有)。函数模板特化的签名包括其特化的模板的签名及其模板参数(无论是显式指定还是推导)。[ 笔记:

回答by Mark Ransom

The function signature doesn't include the return type or linkage type of the function.

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

OK, Wikipediadisagrees 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?

好吧,维基百科不同意我对包含的返回类型的看法。但是我知道在决定函数调用是否与签名匹配时,编译器不使用返回类型。之前的 StackOverflow 问题似乎同意:返回类型是函数签名的一部分吗?

回答by Ben Voigt

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.

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

e.g.

例如

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

has the same signature as

具有相同的签名

void f(int, const char*);

回答by t0mm13b

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:

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

  • It returns a datatype of int
  • Two parameters are also of datatype of int, named aand brespectively
  • 它返回一个数据类型 int
  • 两个参数的数据类型也intab分别命名为和

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

另一方面,函数原型是在 C/C++ 编译器中提供线索,关于预期的内容,如果签名与原型不匹配,编译器将在“函数声明错误”或“函数声明错误”的上下文中发出错误'原型不匹配'。