pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

返回類(lèi)型是函數(shù)簽名的一部分嗎?

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

問(wèn)題描述

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

解決方案

普通函數(shù)的簽名中不包括返回類(lèi)型.

(注意:我已經(jīng)重寫(xiě)了這個(gè)答案,下面的評(píng)論不適用于這次修訂——詳情請(qǐng)參見(jiàn)編輯歷史).

簡(jiǎn)介

然而,標(biāo)準(zhǔn)中關(guān)于函數(shù)和函數(shù)聲明的問(wèn)題很復(fù)雜.有兩層必須考慮:

  • 聲明
  • 實(shí)體

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

標(biāo)準(zhǔn)在1.3.10中定義了一個(gè)函數(shù)的簽名,包括以下內(nèi)容:

<塊引用>

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

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

<塊引用>

函數(shù)模板特化的簽名由函數(shù)模板的簽名和實(shí)際的模板參數(shù)(無(wú)論是明確指定的還是推導(dǎo)的)組成.

函數(shù)模板的簽名由函數(shù)簽名、返回類(lèi)型和模板參數(shù)列表組成.

那么簽名到底包含什么?

因此,當(dāng)我們?cè)儐?wèn)函數(shù)的簽名時(shí),我們必須給出兩個(gè)答案:

  • 對(duì)于函數(shù)模板的特化函數(shù),簽名包括返回類(lèi)型.
  • 對(duì)于非特化函數(shù),返回類(lèi)型不是簽名的一部分.

但是請(qǐng)注意,無(wú)論如何,返回類(lèi)型是函數(shù)類(lèi)型的重要組成部分.也就是說(shuō),以下內(nèi)容無(wú)效:

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

如果只有返回類(lèi)型不同,重載何時(shí)無(wú)效?

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

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

但接受以下代碼:

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

然而,標(biāo)準(zhǔn)確實(shí)禁止只在返回類(lèi)型上不同的函數(shù)聲明(定義重載何時(shí)有效,何時(shí)無(wú)效).它沒(méi)有準(zhǔn)確定義僅因返回類(lèi)型而不同"的內(nèi)容.意思是,不過(guò).


標(biāo)準(zhǔn)段落引用:

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

作為參考,這里是最新的 C++0x 草案 n3000 關(guān)于簽名"的說(shuō)明.在 1.3.11 中,它對(duì)不同類(lèi)型實(shí)體的覆蓋要完整得多:

<塊引用>

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

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 ]

這篇關(guān)于返回類(lèi)型是函數(shù)簽名的一部分嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 电子巡更系统-巡检管理系统-智能巡检【金万码】 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 脱硫搅拌器厂家-淄博友胜不锈钢搅拌器厂家 | 派财经_聚焦数字经济内容服务平台 | 氢氧化钾厂家直销批发-济南金昊化工有限公司 | 臭氧实验装置_实验室臭氧发生器-北京同林臭氧装置网 | 冷水机-冰水机-冷冻机-冷风机-本森智能装备(深圳)有限公司 | 飞歌臭氧发生器厂家_水处理臭氧发生器_十大臭氧消毒机品牌 | 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 郑州巴特熔体泵有限公司专业的熔体泵,熔体齿轮泵与换网器生产厂家 | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 杭州|上海贴标机-百科| 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 软文推广发布平台_新闻稿件自助发布_媒体邀约-澜媒宝 | 西安标准厂房_陕西工业厂房_西咸新区独栋厂房_长信科技产业园官方网站 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 自恢复保险丝_贴片保险丝_力特保险丝_Littelfuse_可恢复保险丝供应商-秦晋电子 | 定制防伪标签_防伪标签印刷_防伪标签厂家-510品保防伪网 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | 匀胶机旋涂仪-声扫显微镜-工业水浸超声-安赛斯(北京)科技有限公司 | 仿清水混凝土_清水混凝土装修_施工_修饰_保护剂_修补_清水混凝土修复-德州忠岭建筑装饰工程 | 皮带输送机-大倾角皮带输送机-皮带输送机厂家-河南坤威机械 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 刑事律师_深圳著名刑事辩护律师_王平聚【清华博士|刑法教授】 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 涂层测厚仪_光泽度仪_uv能量计_紫外辐照计_太阳膜测试仪_透光率仪-林上科技 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 工作服定制,工作服定做,工作服厂家-卡珀职业服装(苏州)有限公司 | 民用音响-拉杆音响-家用音响-ktv专用音响-万昌科技 |