Edit

Share via


Compiler Error C3495

'var': a lambda capture must have automatic storage duration

You cannot capture a variable that does not have automatic storage duration, such as a variable that is marked static or extern.

To correct this error

  • Do not pass a static or extern variable to the capture list of the lambda expression.

Example

The following example generates C3495 because the static variable n appears in the capture list of a lambda expression:

// C3495.cpp

int main()
{
   static int n = 66;
   [&n]() { return n; }(); // C3495
}

See also

Lambda Expressions