Compiler Error C3492

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 C3492.

var': you cannot capture a member of an anonymous union

You cannot capture a member of an unnamed union.

To correct this error

  • Give the union a name and pass the complete union structure to the capture list of the lambda expression.

The following example generates C3492 because it captures a member of an anonymous union:

// C3492a.cpp  
  
int main()  
{  
   union  
   {  
      char ch;  
      int x;  
   };  
  
   ch = 'y';  
   [&x](char ch) { x = ch; }(ch); // C3492  
}  

The following example resolves C3492 by giving the union a name and by passing the complete union structure to the capture list of the lambda expression:

// C3492b.cpp  
  
int main()  
{  
   union  
   {  
      char ch;  
      int x;  
   } u;  
  
   u.ch = 'y';  
   [&u](char ch) { u.x = ch; }(u.ch);  
}  

Lambda Expressions

Show: