14

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)

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 Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

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. [...]

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.

Which paragraph would prohibit this?

4

2 回答 2

9

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

如果声明符列表包含多个声明符,则每个声明变量的类型都按上述方式确定。如果每次推导时为模板参数 U 推导的类型都不相同,则程序是非良构的。

于 2013-05-20T04:19:00.237 回答
2

我找到了更正的措辞(它是最终 草案和官方标准之间实际上不同的措辞之一)。

7.1.6.4-7 如果声明符列表包含多个声明符,则每个声明变量的类型都按上述确定。如果每次推导时为模板参数 U 推导的类型都不相同,则程序是非良构的。

其中“U”在上一段中被描述为用于扣除每个参数的发明类型。这是对草案的不幸更改,因为它本来是一个非常好的功能。(不过,我也可能误解了标准中的前一段,因为它也处理 std::initializer_list)

于 2013-05-20T04:10:00.190 回答