cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

Lambdas have members, and they are weird

Open lefticus opened this issue 4 days ago • 0 comments

This is a mocked-up fix to cpp-insight's version of what a lambda is. We need to talk about members for lambdas and how they are initialized.

auto make_lambda() {
  int x = 42;
      
  class __lambda_4_10
  {
    public: 
    inline /*constexpr */ int operator()(int i) const
    {
      return __x + i;
    }
    
    int __x = x;
    
  } __lambda_4_10{};
  
  return __lambda_4_10;
}


int main()
{
  auto l = make_lambda();
  // this line of code fails because the
  // object I'm trying to initialize
  // the member from no longer exists
  // in this scope
  // decltype(l) new_lambda;

  return l(2);
}

lefticus avatar Dec 02 '25 16:12 lefticus