为什么“auto”声明都是相同类型? [英] Why must 'auto' declarations all be of the same type?

查看:129
本文介绍了为什么“auto”声明都是相同类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎不允许使用 auto 关键字声明不同类型的多个变量。

It appears that it is not allowed to declare multiple variables of distinct types using the auto keyword. I can't figure out the wording in the standard that would prevent it however.

auto i = 1, j = 1.0; //deduction failure (several compilers)

历史上我明白,因为你只有一个< specifier-spec 。然而,标准中的规则似乎并不排除,事实上他们鼓励, auto 可以是每个的不同类型。考虑这些段落:

Historically I understand since you have only one decl-specifier-spec. However, the rules in the standard don't seem to preclude, in fact they encourage, that auto can be a distinct type for each. Consider these paragraphs:


8-3每个声明中的 init-declarator

7.1.6.4-7如果声明器列表包含多个声明器,则每个声明变量的类型被确定为
as如上所述。 [...]

7.1.6.4-7 If the list of declarators contains more than one declarator, the type of each declared variable is determined as described above. [...]

即使没有 auto ,相同类型,因为某些修饰符如 * 可以单独应用于每个声明器。对我来说,现在看来,该措辞允许每个 auto 声明符是一个完全不同的类型。

Even without auto not all variables needed to have the same type, as certain modifiers like * could be applied to each declarator individually. To me it appears now that the wording allows each auto declarator to be a completely distinct type.

禁止此操作?

推荐答案

类型扣除对列表中的每个对象执行,但最终结果必须是单个类型 [dcl.spec.auto] / 7 (强调我的):

Type deduction is performed for every object in the list, but the final result must be a single type [dcl.spec.auto]/7 (emphasis mine):


的声明符包含多个声明符,每个声明变量的类型如上所述确定。 如果在每次扣减中,对于模板参数U推导的类型不相同,则该程序不成立。

这篇关于为什么“auto”声明都是相同类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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