This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-10-26


1347. Consistency of auto in multiple-declarator declarations

Section: 9.2.9.7  [dcl.spec.auto]     Status: CD3     Submitter: Richard Smith     Date: 2011-08-16

[Voted into the WP at the February, 2012 meeting; moved to DR at the October, 2012 meeting.]

The intent of 9.2.9.7 [dcl.spec.auto] paragraph 7 appears to have been that the type represented by auto should be the same for each declarator in the declaration. However, the current wording does not achieve that goal. For example, in

    auto a = 0, b = { 1, 2, 2 };

the auto specifier represents int in the first declarator and std::initializer_list<int> in the second. (See also issue 1265.)

Proposed resolution (August, 2011):

Move the example in 9.2.9.7 [dcl.spec.auto] paragraph 7 into that of paragraph 6 and change paragraph 7 as follows:

...[Example:

  auto x1 = { 1, 2 };   // decltype(x1) is std::initializer_list<int>
  auto x2 = { 1, 2.0 }; // error: cannot deduce element type

  const auto &i = expr;

The type of i is the deduced type of the parameter u in the call f(expr) of the following invented function template:

  template <class U> void f(const U& u);

end example]

If the list of declarators init-declarator-list contains more than one declarator init-declarator, the type of each declared variable is determined as described above. If the type deduced for the template parameter U that replaces the occurrence of auto is not the same in each deduction, the program is ill-formed.

[Example:

  const auto &i = expr;

The type of i is the deduced type of the parameter u in the call f(expr) of the following invented function template:

  template <class U> void f(const U& u);
  auto x = 5, *y = &x;       // OK: auto is int
  auto a = 5, b = { 1, 2 };  // error: different types for auto

end example]