Compiler Error C3498
Visual Studio 2015
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Compiler Error C3498.
var': you cannot capture a variable that has a managed or WinRTtype
You cannot capture a variable that has a managed type or a Windows Runtime type in a lambda.
To correct this error
- Pass the managed or Windows Runtime variable to the parameter list of the lambda expression.
The following example generates C3498 because a variable that has a managed type appears in the capture list of a lambda expression:
// C3498a.cpp
// compile with: /clr
using namespace System;
int main()
{
String ^ s = "Hello";
[&s](String ^ r)
{ return String::Concat(s, r); } (", World!"); // C3498
}
The following example resolves C3498 by passing the managed variable s to the parameter list of the lambda expression:
// C3498b.cpp
// compile with: /clr
using namespace System;
int main()
{
String ^ s = "Hello";
[](String ^ s, String ^ r)
{ return String::Concat(s, r); } (s, ", World!");
}
Show: