不同翻译单元中的不可重载非内联函数定义 [英] Non-overloadable non-inline function definitions in different translation units

查看:103
本文介绍了不同翻译单元中的不可重载非内联函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有2个TU,带有2个带有外部链接的非内联函数定义,它们的返回类型不同.
我的程序违反了哪些段落?
[basic.def.odr]/4 说:

Let's say I have 2 TUs with 2 non-inline function definitions with external linkage which differ only in their return types.
Which paragraph(s) my program violates?
[basic.def.odr]/4 says:

每个程序应在被丢弃的语句之外,确切地包含该程序中奇特使用的每个非内联函数或变量的一个定义;无需诊断.

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required.

但是

  1. 本段说已被滥用" ,可能会,也可能不会.
  2. 如何知道我是否在不同的TU中定义了相同的非内联函数? [over.dcl]/1 涉及相同的范围
  1. This paragraph says "that is odr-used" which may or may not be the case.
  2. How do I tell if I define the same non-inline function in different TUs, after all? [over.dcl]/1 speaks about the same scope.

推荐答案

我相信您正在寻找的是: [basic.link]/9 :

I believe you're looking for: [basic.link]/9:

如果两个名称相同([basic.pre])并且在不同作用域中声明,则它们应表示相同的变量,函数,类型,模板或名称空间(如果存在)

Two names that are the same ([basic.pre]) and that are declared in different scopes shall denote the same variable, function, type, template or namespace if

    两个名称都具有外部或模块链接,并在附加到同一模块的声明中声明,或者两个名称都具有内部链接,并在相同的翻译单元中声明;和
  • 名称均指相同名称空间的成员或相同类的成员(不是通过继承);和
  • 当两个名称都表示函数或函数模板时,签名([defns.signature],[defns.signature.templ])是相同的.
  • both names have external or module linkage and are declared in declarations attached to the same module, or else both names have internal linkage and are declared in the same translation unit; and
  • both names refer to members of the same namespace or to members, not by inheritance, of the same class; and
  • when both names denote functions or function templates, the signatures ([defns.signature], [defns.signature.templ]) are the same.

如果具有外部链接的相同名称的多个声明将声明同一实体(除了将它们附加到不同的模块之外),则程序格式错误;无需诊断. [注意:using-declaration,typedef声明和alias-declaration不声明实体,而只是引入同义词.同样,using指令也不会声明实体. —尾注]

If multiple declarations of the same name with external linkage would declare the same entity except that they are attached to different modules, the program is ill-formed; no diagnostic is required. [ Note: using-declarations, typedef declarations, and alias-declarations do not declare entities, but merely introduce synonyms. Similarly, using-directives do not declare entities. — end note ]

[basic.link]/11 :

在对类型进行所有调整之后(在其typedef被其定义替换之前),所有引用给定变量或函数的声明所指定的类型应相同,不同之处在于数组对象的声明可以指定不同的数组类型.是否存在主数组绑定([dcl.array]).违反此类型身份规则的规则不需要诊断.

After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, except that declarations for an array object can specify array types that differ by the presence or absence of a major array bound ([dcl.array]). A violation of this rule on type identity does not require a diagnostic.

[defns.signature] :

功能"名称,参数类型列表([dcl.fct])和封闭的名称空间(如果有)

⟨function⟩ name, parameter-type-list ([dcl.fct]), and enclosing namespace (if any)

返回类型不是签名的一部分,因此您违反了相同签名意味着相同实体的规则.

The return type isn't part of the signature, so you're violating the rule that same signature means same entity.

通常来说,在戴维斯(Davis)我们不配的英雄"出台之前,标准中有关范围和名称查找的所有讨论都被打破了.鲱鱼的工作正在进行.

Generally speaking, all discussions of scope and name lookup in the standard are pretty broken until Davis "The Hero We Don't Deserve" Herring's work goes through.

这篇关于不同翻译单元中的不可重载非内联函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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