<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 696</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="696"></A><H4>696.
  
Use of block-scope constants in local classes
</H4>
<B>Section: </B>11.6&#160; [<A href="https://wg21.link/class.local">class.local</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Steve Adamczyk
 &#160;&#160;&#160;

 <B>Date: </B>29 May, 2008<BR>


<P>[Voted into the WP at the March, 2011 meeting.]</P>

<P>According to 11.6 [<A href="https://wg21.link/class.local#1">class.local</A>] paragraph 1,</P>

<BLOCKQUOTE>

Declarations in a local class can use only type names, static variables,
extern variables and functions, and enumerators from the enclosing scope.

</BLOCKQUOTE>

<P>This would presumably make both of the members of <TT>S2</TT> below
ill-formed:</P>

<PRE>
    void test () {
      const int local_const = 7;
      struct S2 {
        int member:local_const;
        void f() { int j = local_const; }
      };
    }
</PRE>

<P>Should there be an exception to this rule for constant values?
Current implementations seem to accept the reference to
<TT>local_const</TT> in the bit-field declaration but not in the
member function definition.  Should they be the same or different?</P>

<P><B>Notes from the September, 2008 meeting:</B></P>

<P>The CWG agreed that both uses of <TT>local_const</TT> in the
example above should be accepted.  The intent of the restriction
was to avoid the need to pass a frame pointer into local class
member functions, so uses of local const variables as values
should be permitted.</P>

<P><B>Notes from the October, 2009 meeting:</B></P>

<P>There was interest in an approach that would allow
explicitly-captured constants to appear in constant expressions
but also to be &#8220;used.&#8221; Another suggestion was to have
variables captured if they appear in either &#8220;use&#8221; or
&#8220;non-use&#8221; contexts.</P>

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

<OL>
<LI><P>Change 7.5.6 [<A href="https://wg21.link/expr.prim.lambda#17">expr.prim.lambda</A>] paragraph 17 as
follows:</P></LI>

<BLOCKQUOTE>

<P>Every <I>id-expression</I> that is an odr-use (6.3 [<A href="https://wg21.link/basic.def.odr">basic.def.odr</A>]) of an entity captured by copy is transformed into an
access to the corresponding unnamed data member of the closure
type. <INS>[<I>Note:</I> an <I>id-expression</I> that is not an
odr-use refers to the original entity, never to a member of the
closure type. Furthermore, such an <I>id-expression</I> does not cause
the implicit capture of the entity. &#8212;<I>end note</I>]</INS> If
<TT>this</TT> is captured, each odr-use of <TT>this</TT> is
transformed into an access to the corresponding unnamed data member of
the closure type, cast (7.6.3 [<A href="https://wg21.link/expr.cast">expr.cast</A>]) to the type of
<TT>this</TT>. [<I>Note:</I> the cast ensures that the transformed
expression is a prvalue. &#8212;<I>end note</I>]
<INS>[<I>Example:</I></INS>
</P>

<PRE>
<INS>  void f(const int*);
  void g() {
    const int N = 10;
    [=] {
      int arr[N];    //<SPAN CLASS="cmnt"> OK: not an odr-use, refers to automatic variable</SPAN>
      f(&amp;N);         //<SPAN CLASS="cmnt"> OK: causes </SPAN>N<SPAN CLASS="cmnt"> to be captured; </SPAN>&amp;N<SPAN CLASS="cmnt"> points to the</SPAN>
                     //<SPAN CLASS="cmnt"> corresponding member of the closure type</SPAN>
    }
  }</INS>
</PRE>

<P><INS>&#8212;<I>end example</I>]</INS></P>

</BLOCKQUOTE>

<LI>Change 11.6 [<A href="https://wg21.link/class.local#1">class.local</A>] paragraph 1 as follows:</LI>

<BLOCKQUOTE>

<P>...Declarations in a local class <DEL>can use only type names,
static variables, extern variables and functions, and enumerators from
the</DEL> <INS>shall not odr-use (6.3 [<A href="https://wg21.link/basic.def.odr">basic.def.odr</A>]) a variable
with automatic storage duration from an</INS> enclosing
scope. [<I>Example:</I>
</P>

<PRE>
  int x;
  void f() {
    static int s ;
    int x;
<INS>    const int N = 5;</INS>
    extern int <DEL>g</DEL> <INS>q</INS>();

    struct local {
      int g() { return x; }     //<SPAN CLASS="cmnt"> error: <INS>odr-use of automatic variable</INS> </SPAN>x<DEL><SPAN CLASS="cmnt"> has automatic storage duration</SPAN></DEL>
      int h() { return s; }     //<SPAN CLASS="cmnt"> OK</SPAN>
      int k() { return ::x; }   //<SPAN CLASS="cmnt"> OK</SPAN>
      int l() { return <DEL>g</DEL> <INS>q</INS>(); } //<SPAN CLASS="cmnt"> OK</SPAN>
<INS>      int m() { return N; }     //<SPAN CLASS="cmnt"> OK: not an odr-use</SPAN>
      int* n() { return &amp;N; }   //<SPAN CLASS="cmnt"> error: odr-use of automatic variable </SPAN>N</INS>
    };
  }

  local* p = 0;                 //<SPAN CLASS="cmnt"> error: </SPAN>local<SPAN CLASS="cmnt"> not in scope</SPAN>
</PRE>

<P>&#8212;<I>end example</I>]</P>

</BLOCKQUOTE>

</OL>

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