如果我不使用odr,可以在翻译单元中对它进行多个定义吗? [英] If I don't odr-use a variable, can I have multiple definitions of it across translation units?

查看:89
本文介绍了如果我不使用odr,可以在翻译单元中对它进行多个定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该标准似乎暗示着,如果不是 odr用过的(第3.2/3节),则对变量的定义数量没有限制:

The standard seems to imply that there is no restriction on the number of definitions of a variable if it is not odr-used (§3.2/3):

每个程序应准确地包含该程序中使用的每个非内联函数或变量的一个定义;无需诊断.

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

它确实说不能在翻译单元(第3.2/1节)中多次定义任何变量:

It does say that any variable can't be defined multiple times within a translation unit (§3.2/1):

任何变量,函数,类类型,枚举类型或模板的翻译单元均不得包含多个定义.

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

但是我找不到整个程序中非奇数变量的限制.那么为什么我不能编译如下内容:

But I can't find a restriction for non-odr-used variables across the entire program. So why can't I compile something like the following:

// other.cpp
int x;

// main.cpp
int x;
int main() {}

使用g ++ 4.6.3编译和链接这些文件,我得到multiple definition of 'x'的链接器错误.老实说,我期望如此,但是由于x并没有在任何地方使用(据我所知),所以我看不出该标准如何限制了这一点.还是不确定的行为?

Compiling and linking these files with g++ 4.6.3, I get a linker error for multiple definition of 'x'. To be honest, I expect this, but since x is not odr-used anywhere (as far as I can tell), I can't see how the standard restricts this. Or is it undefined behaviour?

推荐答案

您的程序违反了链接规则. C ++ 11§3.5[basic.link]/9状态:

Your program violates the linkage rules. C++11 §3.5[basic.link]/9 states:

两个相同且在不同作用域中声明的名称应表示相同 变量,函数,类型,枚举器,模板或名称空间(如果存在)

Two names that are the same and that are declared in different scopes shall denote the same variable, function, type, enumerator, template or namespace if

  • 两个名称都具有外部链接,或者两个名称都具有内部链接,并且在同一翻译单元中声明;和

  • both names have external linkage 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, the parameter-type-lists of the functions are identical; and

当两个名称都表示功能模板时,签名是相同的.

when both names denote function templates, the signatures are the same.

(我引用了完整的段落作为参考.后两个项目符号在这里不适用.)

(I've cited the complete paragraph, for reference. The second two bullets do not apply here.)

在您的程序中,有两个名称x,它们是相同的.它们在不同的范围内声明(在这种情况下,它们在不同的翻译单元中声明).这两个名称都具有外部链接,并且两个名称都引用相同名称空间(全局名称空间)的成员.

In your program, there are two names x, which are the same. They are declared in different scopes (in this case, they are declared in different translation units). Both names have external linkage and both names refer to members of the same namespace (the global namespace).

这两个名称表示相同的变量.声明int x;定义了一个变量.因为程序中有两个这样的定义,所以程序中有两个变量.一个翻译单位中的名称"x"表示这些变量之一;另一个翻译单元中的名称"x"表示另一个.因此,该程序格式不正确.

These two names do not denote the same variable. The declaration int x; defines a variable. Because there are two such definitions in the program, there are two variables in the program. The name "x" in one translation unit denotes one of these variables; the name "x" in the other translation unit denotes the other. Therefore, the program is ill-formed.

这篇关于如果我不使用odr,可以在翻译单元中对它进行多个定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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