不同翻译单元中不可重载的非内联函数定义
Non-overloadable non-inline function definitions in different translation units
假设我有 2 个 TU,其中有 2 个带有外部链接的非内联函数定义,它们仅在 return 类型上有所不同。
我的程序违反了哪一段?
[basic.def.odr]/4 说:
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.
但是
- 这一段说的是 "that is odr-used" 这可能是也可能不是。
- 毕竟,我如何判断我是否在不同的 TU 中定义了相同的非内联函数? [over.dcl]/1 谈到相同的范围。
我相信您正在寻找:[basic.link]/9:
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
- 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.
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 ]
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.
⟨function⟩ name, parameter-type-list ([dcl.fct]), and enclosing namespace (if any)
return 类型不是签名的一部分,因此您违反了相同签名表示相同实体的规则。
一般来说,在戴维斯“我们不值得拥有的英雄”赫林的工作完成之前,标准中所有关于范围和名称查找的讨论都被打破了。
假设我有 2 个 TU,其中有 2 个带有外部链接的非内联函数定义,它们仅在 return 类型上有所不同。
我的程序违反了哪一段?
[basic.def.odr]/4 说:
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.
但是
- 这一段说的是 "that is odr-used" 这可能是也可能不是。
- 毕竟,我如何判断我是否在不同的 TU 中定义了相同的非内联函数? [over.dcl]/1 谈到相同的范围。
我相信您正在寻找:[basic.link]/9:
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
- 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.
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 ]
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.
⟨function⟩ name, parameter-type-list ([dcl.fct]), and enclosing namespace (if any)
return 类型不是签名的一部分,因此您违反了相同签名表示相同实体的规则。
一般来说,在戴维斯“我们不值得拥有的英雄”赫林的工作完成之前,标准中所有关于范围和名称查找的讨论都被打破了。