<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 1591</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="1591"></A><H4>1591.
  
Deducing array bound and element type from initializer list
</H4>
<B>Section: </B>13.10.3.2&#160; [<A href="https://wg21.link/temp.deduct.call">temp.deduct.call</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD4
 &#160;&#160;&#160;

 <B>Submitter: </B>Peter Dimov
 &#160;&#160;&#160;

 <B>Date: </B>2012-12-01<BR>


<P>[Moved to DR at the November, 2014 meeting.]</P>



<P>Currently, 13.10.3.2 [<A href="https://wg21.link/temp.deduct.call#1">temp.deduct.call</A>] paragraph 1 says,</P>

<BLOCKQUOTE>

Template argument deduction is done by comparing each function
template parameter type (call it <TT>P</TT>) with the type of the
corresponding argument of the call (call it <TT>A</TT>) as described
below.  If removing references and cv-qualifiers from <TT>P</TT> gives
<TT>std::initializer_list&lt;</TT><I>P'</I><TT>&gt;</TT> for some
<I>P'</I> and the argument is an initializer list (9.5.5 [<A href="https://wg21.link/dcl.init.list">dcl.init.list</A>]), then deduction is performed instead for each element
of the initializer list, taking <I>P'</I> as a function template
parameter type and the initializer element as its argument. Otherwise,
an initializer list argument causes the parameter to be considered a
non-deduced context (13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>]).

</BLOCKQUOTE>

<P>It would seem reasonable, however, to allow an array bound to be
deduced from the number of elements in the initializer list, e.g.,</P>

<PRE>
  template&lt;int N&gt; void g(int const (&amp;)[N]);
  void f() {
    g( { 1, 2, 3, 4 } );
  }
</PRE>

<P><B>Additional note (March, 2013):</B></P>

<P>The element type should also be deducible.
</P>

<P><B>Proposed resolution (February, 2014):</B></P>

<OL>
<LI><P>Change 13.10.3.2 [<A href="https://wg21.link/temp.deduct.call#1">temp.deduct.call</A>] paragraph 1 as
follows:</P></LI>

<BLOCKQUOTE>

<P>Template argument deduction is done by comparing each function template
parameter type (call it <TT>P</TT>) with the type of the corresponding
argument of the call (call it <TT>A</TT>) as described
below. If <INS><TT>P</TT> is a dependent type,</INS> removing references
and cv-qualifiers from <TT>P</TT>
gives <TT>std::initializer_list&lt;</TT><I>P'</I><TT>&gt;</TT> <INS>or
<I>P'</I><TT>[</TT><I>N</I><TT>]</TT></INS> for
some <I>P'</I> <INS>and <I>N</I>,</INS> and the argument is <DEL>an</DEL>
<INS>a non-empty</INS> initializer list (9.5.5 [<A href="https://wg21.link/dcl.init.list">dcl.init.list</A>]), then
deduction is performed instead for each element of the initializer list,
taking <I>P'</I> as a function template parameter type and the initializer
element as its argument<INS>, and in the
<I>P'</I><TT>[</TT><I>N</I><TT>]</TT> case, if <I>N</I> is a non-type
template parameter, <I>N</I> is deduced from the length of the
initializer list</INS>. Otherwise, an initializer list argument causes the
parameter to be considered a non-deduced context
(13.10.3.6 [<A href="https://wg21.link/temp.deduct.type">temp.deduct.type</A>]). [<I>Example:</I>
</P>

<PRE>
  template&lt;class T&gt; void f(std::initializer_list&lt;T&gt;);
  f({1,2,3});              //<SPAN CLASS="cmnt"> </SPAN>T<SPAN CLASS="cmnt"> deduced to </SPAN>int
  f({1,"asdf"});           //<SPAN CLASS="cmnt"> error: </SPAN>T<SPAN CLASS="cmnt"> deduced to both </SPAN>int<SPAN CLASS="cmnt"> and </SPAN>const char*

  template&lt;class T&gt; void g(T);
  g({1,2,3});              //<SPAN CLASS="cmnt"> error: no argument deduced for </SPAN>T

<INS>  template&lt;class T, int N&gt; void h(T const(&amp;)[N]);
  h({1,2,3});              //<SPAN CLASS="cmnt"> </SPAN>T<SPAN CLASS="cmnt"> deduced to </SPAN>int<SPAN CLASS="cmnt">, </SPAN>N<SPAN CLASS="cmnt"> deduced to </SPAN>3

  template&lt;class T&gt; void j(T const(&amp;)[3]);
  j({42});                 //<SPAN CLASS="cmnt"> </SPAN>T<SPAN CLASS="cmnt"> deduced to </SPAN>int<SPAN CLASS="cmnt">, array bound not considered</SPAN>

  struct Aggr { int i; int j; };
  template&lt;int N&gt; void k(Aggr const(&amp;)[N]);
  k({1,2,3});              //<SPAN CLASS="cmnt"> error: deduction fails, no conversion from </SPAN>int<SPAN CLASS="cmnt"> to </SPAN>Aggr
  k({{1},{2},{3}});        //<SPAN CLASS="cmnt"> OK, </SPAN>N<SPAN CLASS="cmnt"> deduced to </SPAN>3

  template&lt;int M, int N&gt; void m(int const(&amp;)[M][N]);
  m({{1,2},{3,4}});        //<SPAN CLASS="cmnt"> </SPAN>M<SPAN CLASS="cmnt"> and </SPAN>N<SPAN CLASS="cmnt"> both deduced to </SPAN>2

  template&lt;class T, int N&gt; void n(T const(&amp;)[N], T);
  n({{1},{2},{3}},Aggr()); //<SPAN CLASS="cmnt"> OK, </SPAN>T<SPAN CLASS="cmnt"> is </SPAN>Aggr<SPAN CLASS="cmnt">, </SPAN>N<SPAN CLASS="cmnt"> is </SPAN>3</INS>
</PRE>

<P>&#8212;<I>end example</I>] For a function parameter pack...</P>

</BLOCKQUOTE>

<LI><P>Change the penultimate bullet of 13.10.3.6 [<A href="https://wg21.link/temp.deduct.type#5">temp.deduct.type</A>] paragraph 5
as follows:</P></LI>

<BLOCKQUOTE>

<P>The non-deduced contexts are:</P>

<UL>
<LI><P>...</P></LI>

<LI><P>A function parameter for which the associated argument is an initializer
list (9.5.5 [<A href="https://wg21.link/dcl.init.list">dcl.init.list</A>]) but the parameter does not have
<DEL><TT>std::initializer_list</TT> or reference to possibly cv-qualified
<TT>std::initializer_list</TT> type</DEL> <INS>a type for which deduction
from an initializer list is specified
(13.10.3.2 [<A href="https://wg21.link/temp.deduct.call">temp.deduct.call</A>])</INS>. [<I>Example:</I>...</P></LI>

<LI><P>A function parameter pack that does not occur at the end of
the <I>parameter-declaration-list</I>.</P></LI>

</UL>

</BLOCKQUOTE>

</OL>

<BR><BR>
</BODY>
</HTML>
