------- Comment #1 from jakub at gcc dot gnu dot org  2010-07-27 15:14 -------
I've briefly looked at this.  For !processing_template_decl, it shouldn't be
hard to walk LAMBDA_EXPR_CAPTURE_LIST e.g. in cp_parser_lambda_expression
and add copy_decl of the vars from the capture list with DECL_VALUE_EXPR
pointing to this->__fieldname or *this->__fieldname.  Templates slightly
complicate that though, as if this was to be done say in build_lambda_object
guarded with !processing_template_decl, lambda_function probably doesn't have
body instantiated yet.  And if cp_parser_lambda_expression does this
unconditionally, we'd need to handle tsubsting DECL_VALUE_EXPRs.
Or we could add them during genericization of the lambda function, but we'd
then need to be able to find the corresponding LAMBDA_EXPR from the
FUNCTION_DECL.

Another thing is whether it is right to call the __lambda* argument this.  Even
when it is artificial, it is still visible to the user in the debugger. 
Shouldn't it be made DECL_NAMELESS once the vars with DECL_VALUE_EXPR are
added?

Jason, what do you prefer here?

Testcase I was playing with:

extern "C" void abort ();

template<typename F>
F
foo (int *x, int *y, F f)
{
  for (; x != y; ++x)
    f (*x);
  return f;
}

template<typename T>
void
bar (T *w)
{
  T s = 0, t = 0, u = 0, v = 10;

  foo (&w[0], &w[10], [&s, &t, u, v] (T &a) -> void
    {
      s += a;
      t += s + 10 - v;
    });

  if (s != 45 || t != 165)
    abort ();
}

int
main ()
{
  int s = 0, t = 0, u = 10, v = 10;
  int w[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

  foo (&w[0], &w[10], [&s, &t, u, v] (int &a) -> void
    {
      s += a;
      t += s + 10 - v;
    });

  if (s != 45 || t != 165)
    abort ();

  bar (w);
  return 0;
}


-- 

jakub at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2010-07-27 15:14:11
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43912

Reply via email to