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

2024-04-25


2149. Brace elision and array length deduction

Section: 9.4.2  [dcl.init.aggr]     Status: DR     Submitter: Vinny Romano     Date: 2015-06-25

[Accepted as a DR as paper P3106R1 at the March, 2024 meeting.]

According to 9.4.2 [dcl.init.aggr] paragraph 4,

An array of unknown size initialized with a brace-enclosed initializer-list containing n initializer-clauses, where n shall be greater than zero, is defined as having n elements (9.3.4.5 [dcl.array]).

However, the interaction of this with brace elision is not clear. For instance, in the example in paragraph 7,

  struct X { int i, j, k = 42; };
  X a[] = { 1, 2, 3, 4, 5, 6 };
  X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };

a and b are said to have the same value, even though there are six initializer-clauses in the initializer list in a's initializer and two in b's initializer.

Similarly, 13.10.3.2 [temp.deduct.call] paragraph 1 says,

in the P'[N] case, if N is a non-type template parameter, N is deduced from the length of the initializer list

Should that take into account the underlying type of the array? For example,

  template<int N> void f1(const X(&)[N]);
  f1({ 1, 2, 3, 4, 5, 6 }); // Is N deduced to 2 or 6?

  template<int N> void f2(const X(&)[N][2]);
  f2({ 1, 2, 3, 4, 5, 6 }); // Is N deduced to 1 or 6?

Additional notes (April, 2024)

The situation with arrays of unknown bound was clarified by P3106R1. The concern about template argument deduction was left untouched; the existing wording in 13.10.3.2 [temp.deduct.call] paragraph 1 seems to be clear, rendering the two examples shown above ill-formed, because deduction of P' against the integer arguments fails.