久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

返回類型是函數簽名的一部分嗎?

Is the return type part of the function signature?(返回類型是函數簽名的一部分嗎?)
本文介紹了返回類型是函數簽名的一部分嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 C++ 中,返回類型是否被視為函數簽名的一部分?不允許重載,只修改返回類型.

解決方案

普通函數的簽名中不包括返回類型.

(注意:我已經重寫了這個答案,下面的評論不適用于這次修訂——詳情請參見編輯歷史).

簡介

然而,標準中關于函數和函數聲明的問題很復雜.有兩層必須考慮:

  • 聲明
  • 實體

所謂的函數聲明可以聲明一個函數實體或一個模板實體.如果聲明了一個函數實體,那么您要么必須對函數模板(指定所有參數)進行顯式特化,要么必須聲明一個普通函數.如果聲明了模板實體,則您聲明的是主函數模板,或未指定某些參數的顯式特化.(這與對象聲明"和對象或引用的關系非常相似:前者可以聲明一個對象或一個引用.因此對象聲明不一定聲明一個對象!).

標準在1.3.10中定義了一個函數的簽名,包括以下內容:

<塊引用>

其參數的類型,如果函數是類成員,函數本身和聲明成員函數的類的 cv- 限定符(如果有).函數模板特化的簽名包括其模板參數的類型.(14.5.5.1)

它在這個定義中缺少返回類型,它是函數模板特化簽名的一部分(即聲明一個函數是模板特化的函數聲明),如所指出的14.5.5.1(最近的 C++0x 工作文件修復了這個問題,也提到了 1.3.10 中的返回類型):

<塊引用>

函數模板特化的簽名由函數模板的簽名和實際的模板參數(無論是明確指定的還是推導的)組成.

函數模板的簽名由函數簽名、返回類型和模板參數列表組成.

那么簽名到底包含什么?

因此,當我們詢問函數的簽名時,我們必須給出兩個答案:

  • 對于函數模板的特化函數,簽名包括返回類型.
  • 對于非特化函數,返回類型不是簽名的一部分.

但是請注意,無論如何,返回類型是函數類型的重要組成部分.也就是說,以下內容無效:

void f();int (*pf)() = &f;//不同種類!

如果只有返回類型不同,重載何時無效?

主要編譯器目前拒絕以下代碼:

int f();雙 f();//無效的

但接受以下代碼:

templateint f();模板雙 f();//無效的?

然而,標準確實禁止只在返回類型上不同的函數聲明(定義重載何時有效,何時無效).它沒有準確定義僅因返回類型而不同"的內容.意思是,不過.


標準段落引用:

  • 何時可以重載函數聲明:13.1
  • 什么是函數聲明:7/27/5
  • 函數模板/特化的簽名是什么:14.5.5.1

作為參考,這里是最新的 C++0x 草案 n3000 關于簽名"的說明.在 1.3.11 中,它對不同類型實體的覆蓋要完整得多:

<塊引用>

函數的名稱和參數類型列表 (8.3.5),以及它所屬的類或命名空間.如果函數或函數模板是類成員,則其簽名還包括函數或函數模板本身的 cv 限定符(如果有)和 ref 限定符(如果有).函數模板的簽名還包括其返回類型和模板參數列表.函數模板特化的簽名包括其特化的模板的簽名及其模板參數(無論是顯式指定還是推導).[ 注意:簽名用作名稱修改和鏈接的基礎.— 尾注 ]

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.

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

Introduction

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

  • Declarations
  • Entities

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

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

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)

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:

  • For functions that are specializations of function templates, the signature includes the return type.
  • For functions that are not specializations, the return type is not part of the signature.

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

But accept the following code:

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

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.


Standard paragraph references:

  • 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

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:

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-quali?ers (if any) and the ref-quali?er (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 speci?ed or deduced). [ Note: Signatures are used as a basis for name mangling and linking. — end note ]

這篇關于返回類型是函數簽名的一部分嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 日韩欧美在线视频 | 黄色片网站在线观看 | 中文字幕亚洲欧美日韩在线不卡 | 国产日批 | 在线伊人 | 91精品国产高清一区二区三区 | 综合九九| 韩日免费视频 | 久久久噜噜噜久久中文字幕色伊伊 | 久久久久精 | 成人伊人 | 欧美精品一区二区免费视频 | 天堂一区在线观看 | 久久久久国产精品一区三寸 | 午夜噜噜噜 | 国产精品国产三级国产aⅴ中文 | 亚洲成人a v| 国产精品视频一区二区三区 | 国产成人区| 亚洲精品一区二区久 | 亚洲欧美在线一区 | 最新日韩在线 | 欧美黄 片免费观看 | 丁香综合| 一区二区三区在线 | 欧美精品在线免费 | 国产精品亚洲一区 | 国产精品观看 | 国产午夜精品一区二区 | 五月天婷婷综合 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲伦理自拍 | 亚洲欧美一区二区三区国产精品 | 国产欧美精品 | 91在线看片 | 久久久亚洲一区 | 欧美一区二区三区在线观看 | 久久国产精品网站 | 九色 在线 | 国产不卡视频在线 | 成人福利视频网站 |