是函数签名的返回类型部分吗? [英] Is the return type part of the function signature?

查看:184
本文介绍了是函数签名的返回类型部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,返回类型是函数签名的一部分吗?

In C++, is the return type considered part of the function signature? and no overloading is allowed with just return type modified.

推荐答案

正常函数不在其签名中包括返回类型。

Normal functions do not include the return type in their signature.

注意:我已经重写了此答案,下面的注释不适用于此修订版本 - -history for details)。

(note: i've rewritten this answer, and the comments below don't apply to this revision - see the edit-history for details).

但是,关于函数和函数声明在标准中很复杂。有两个层次需要考虑:

However, the matter about functions and function declarations in the Standard is complicated. There are two layers that have to be considered:


  • 声明

  • 实体

所谓的函数声明可以声明函数实体或模板实体。如果一个函数实体被声明,那么你要么使用一个函数模板(指定所有参数)的显式特化,要么是一个普通函数的声明。如果声明了模板实体,那么您正在声明主函数模板,或者未指定某些参数的显式特殊化。 (这非常类似于对象声明和对象或引用的关系:前者可以声明一个对象或引用,因此对象声明可能不一定声明一个对象!

The so-called function declaration may declare a function entity or a template entity. If a function entity is declared, then you either have to do with an explicit specialization of a function template (with all arguments specified), or a declaration of an ordinary function. If a template entity is declared, then you are declaring a primary function template, or an explicit specialization where some arguments are not specified. (This is very similar to the relation of "object declaration" and objects or references: The former may declare either an object or a reference. So an object declaration may not necessarily declare an object!).

标准定义函数的签名,在 1.3.10 下包含以下内容:

The Standard defines the signature of a function to include the following at 1.3.10:


其参数的类型,如果函数是类成员,则函数本身的cv-限定符(如果有的话)成员函数。函数模板特化的签名包括其模板参数的类型。 (14.5.5.1)

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. The signature of a function template specialization includes the types of its template arguments. (14.5.5.1)

在此定义中缺少返回类型,其中 14.5.5.1 (最近的C ++ 0x工作)指出的函数模板专门化(即声明一个函数是模板的特殊化的函数声明)论文固定,已经提到 1.3.10 中的返回类型):

It's missing the return type in this definition, which is part of the signature of a function template specialization (i.e a function declaration that declares a function which is a specialization of a template), as pointed out by 14.5.5.1 (recent C++0x working papers fixed that already to mention the return type in 1.3.10 too):


函数模板专用化的签名包括函数模板的签名和实际的模板参数(无论是明确指定还是推断)。

The signature of a function template specialization consists of the signature of the function template and of the actual template arguments (whether explicitly specified or deduced).

函数的签名模板由其函数签名,其返回类型及其模板参数列表组成。

The signature of a function template consists of its function signature, its return type and its template parameter list.



那么,签名究竟是什么含有呢?



当我们询问一个函数的签名时,我们必须给出两个答案:

So what exactly does a signature contain, again?

So, when we ask about the signature of a function, we have to give two answers:


  • 函数模板的特殊化,签名包括返回类型。

  • 对于非特殊化的函数,返回类型不是签名的一部分。

但请注意,返回类型在任何情况下都是 的函数。也就是说,以下是无效的:

Notice, however, that the return type, in any case, is a significant part of the type of a function. That is, the following is not valid:

void f();
int (*pf)() = &f; // different types!



何时是重载无效如果只有返回类型不同?



主要编译器目前拒绝以下代码:

When is an overload invalid if only the return type differs?

Major compilers currently reject the following code:

int f();
double f(); // invalid

但接受以下代码:

template<typename T> int f();
template<typename T> double f(); // invalid?

但是,标准禁止一个函数声明, strong>(当定义何时过载有效,以及何时不是)。它并不精确地定义什么只有返回类型不同。

However, the Standard does forbid a function declaration that only differs in the return type (when defining when an overload is valid, and when not). It does not define precisely what "differs only by return type" means, though.


标准段落引用:


  • 函数声明何时可以重载: 13.1 li>
  • 什么是函数声明: 7/2 7/5 li>
  • 函数模板/专业化的签名是什么: 14.5.5.1

  • When can a function declaration be overloaded: 13.1
  • What is a function declaration: 7/2 and 7/5
  • What is the signature of a function template/specialization: 14.5.5.1

为了参考,这里是最近的C ++ 0x草案n3000说到关于签名在 1.3.11 更完整地覆盖不同类型的实体:

For reference, here is what the most recent C++0x draft n3000 says about "signature" in 1.3.11, which is much more complete in its coverage of the different type of entities:


函数的名称和参数类型列表(8.3.5)以及它是其成员的类或命名空间。如果函数或函数模板是类成员,它的签名额外包括函数或函数模板本身的cv-qualial(如果有的话)和ref-qual fi er(如果有的话)。函数模板的签名还包括其返回类型及其模板参数列表。函数模板专用化的签名包括模板的签名,它是专门化的模板,其模板参数(无论明确指定还是推断)。 [注意:签名用作名称调整和链接的基础。 - end note]

the name and the parameter type list (8.3.5) of a function, as well as the class 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 function template additionally includes its return type and its template parameter list. 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 ]

这篇关于是函数签名的返回类型部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆