<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 2247</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="2247"></A><H4>2247.
  
Lambda capture and variable argument list
</H4>
<B>Section: </B>7.5.6.3&#160; [<A href="https://wg21.link/expr.prim.lambda.capture">expr.prim.lambda.capture</A>]
 &#160;&#160;&#160;

 <B>Status: </B>C++17
 &#160;&#160;&#160;

 <B>Submitter: </B>Aaron Ballman
 &#160;&#160;&#160;

 <B>Date: </B>2016-03-11<BR>


<P>[Adopted at the February/March, 2017 meeting.]</P>



<P>Consider:</P>

<PRE>
  #include &lt;cstdarg&gt;
  #include &lt;iostream&gt;

  auto f(int i, ...) {
   return [&amp;]() {
    va_list l;
    va_start(l, i);
    std::cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; ", " &lt;&lt; va_arg(l, int) &lt;&lt; std::endl;
    va_end(l);
   };
  }

  int main() {
   auto l = f(100, 42);
   l();
  }
</PRE>

<P>Is this defined behavior, accessing the variadic arguments passed
to <TT>f</TT>?</P>

<P>
<B>Notes from the December, 2016 teleconference:</B>
</P>

<P>Such examples should have undefined behavior; <TT>va_start</TT>
should only be permitted to access the arguments for the current
function.
</P>

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

<P>Change 17.14.2 [<A href="https://wg21.link/cstdarg.syn#1">cstdarg.syn</A>] paragraph 1 as follows:</P>

<BLOCKQUOTE>

The contents of the header <TT>&lt;cstdarg&gt;</TT> are the same as the C
standard library header <TT>&lt;stdarg.h&gt;</TT>, with the following
changes: The restrictions that ISO C places on the second parameter to
the <TT>va_start()</TT> macro in header <TT>&lt;stdarg.h&gt;</TT> are
different in this International Standard. The parameter <TT>parmN</TT> is
<DEL>the identifier of</DEL> the rightmost parameter in the variable
parameter list of the function definition (the one just before
the <TT>...</TT> ).<SUP>223</SUP> <INS>If the parameter
<TT>parmN</TT> is a pack expansion (13.7.4 [<A href="https://wg21.link/temp.variadic">temp.variadic</A>]) or an
entity resulting from a lambda capture (7.5.6 [<A href="https://wg21.link/expr.prim.lambda">expr.prim.lambda</A>]), the
program is ill-formed, no diagnostic required.</INS> If the
parameter <TT>parmN</TT> is of a reference type, or of a type that is not
compatible with the type that results when passing an argument for which
there is no parameter, the behavior is undefined.

</BLOCKQUOTE>

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