是否使用“自动”匹配使用具体类型说明符的extern声明? [英] Does a declaration using "auto" match an extern declaration that uses a concrete type specifier?

查看:114
本文介绍了是否使用“自动”匹配使用具体类型说明符的extern声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

extern int x;
auto x = 42;
int main() { }

Clang 3.5接受它(现场演示),GCC 4.9和VS2013不(现场演示为前者)。谁是正确的,C ++标准中指定的正确行为在哪里?

Clang 3.5 accepts it (live demo), GCC 4.9 and VS2013 do not (live demo for the former). Who is right, and where is the correct behavior specified in the C++ Standard?

推荐答案

关于我们听说有关重新声明的所有内容:

There's surprisingly little in the standard about this. About all we hear about redeclaration is:


[C ++ 11:3.1 / 1]:声明(第7条)可以将一个或多个名称引入到翻译单元或由先前声明引入的redeclare名称。 [..]

[C++11: 3.1/1]: A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. [..]

c $ c>的语义:

and the only relevant part of auto's semantics:


[C ++ 11:7.1.6.4/3] / code>否则,变量的类型从其初始化器中推导出来。 [..]

(提醒我们 code>是 int )。

我们知道一个变量必须被赋予相同的类型声明:

We know that a variable must be given the same type by all declarations:


[C ++ 11:3.5 / 10]:毕竟类型的调整(其中typedef(7.1.3)由其定义替换),引用给定变量或函数的所有声明指定的类型应该是相同的,除了数组对象的声明可以指定由于存在或不存在主要数组边界(8.3.4)而不同的数组类型。违反这个关于类型身份的规则不需要诊断。

[C++11: 3.5/10]: After all adjustments of types (during which typedefs (7.1.3) 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 (8.3.4). A violation of this rule on type identity does not require a diagnostic.

和所有类型调整应该小心有关 auto 参与所有这些的任何问题;我的解释是,这本质上是 x 在全局范围的有效重新声明(和定义),类型 int ,并且Clang是正确的。即使我们建议 auto 不算作类型调整,因为不需要诊断,在最坏情况下,所有列出的实现都符合自己的方式。

and the "after all adjustments of types" ought to take care of any questions regarding auto's participation in all of this; my interpretation, then, is that this is inherently a valid redeclaration (and definition) of the x at global scope with type int, and that Clang is correct. Even if we propose that auto does not count as "adjustment of type", since no diagnostic is required, at worst all listed implementations are compliant in their own way.

我相信GCC和Visual Studio正在采取以下作为灵感:

I believe GCC and Visual Studio are taking the following as inspiration:


[C ++ 11:7.1.6.4/5]:使用 auto

…但我认为这是短视的。标准语言似乎不可能禁止通常的重新声明规则,只是因为它们不在 7.1.6.4 内重复或明确引用。

…but I think that this is short-sighted. It seems unlikely that the standard language is intended to prohibit the usual redeclaration rules, just because they are not repeated or explicitly referenced from within 7.1.6.4.

C ++ 14增加了与推导的类型的函数声明相关的措辞:

C++14 adds wording that relates to declarations of functions with deduced types:


[C ++ 14:7.1.6.4/13]:具有使用占位符类型的声明返回类型的函数或函数模板的重写声明或特殊化也使用该占位符,而不是推导的类型。 [..]

[C++14: 7.1.6.4/13]: Redeclarations or specializations of a function or function template with a declared return type that uses a placeholder type shall also use that placeholder, not a deduced type. [..]

通过对称,可能会建议在 int 的情况下,意味着GCC和VS在拒绝程序时是正确的。但是,这是一个不同的功能(因为扣除不能应用于纯粹的声明),因此不同的场景。

By symmetry one might suggest that, in your int case, it is intended that GCC and VS be correct in rejecting the program. However, this is a different feature (since deduction cannot be applied to mere declarations) and thus a different scenario.

无论如何,改进的标准措辞将有所帮助。我认为这是一个[相当小的]编辑缺陷。

Either way, improved standard wording would help here. I consider it a [reasonably minor] editorial defect.

这篇关于是否使用“自动”匹配使用具体类型说明符的extern声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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