Nested lambda expressions are very slow to compile and create a huge object file using Visual C ++

I find that nested lambda expressions compile very slowly and generate huge .obj files. For example, on my computer, the following code generates an obj file of 4766 KB in size:

int main() { auto f = [] { auto f = [] { auto f = [] { auto f = [] { auto f = [] { }; }; }; }; }; } 

And the following code (another nesting level is added) will result in error C1128 .

 int main() { auto f = [] { auto f = [] { auto f = [] { auto f = [] { auto f = [] { auto f = [] { }; }; }; }; }; }; } 

They are also very slow to compile. Is there any explanation for this? I am using Visual C ++ 2013.

Update

This seems to be a bug in Visual C ++, I reported it to Microsoft: https://connect.microsoft.com/VisualStudio/feedback/details/813755/nested-lambdas-in-visual-c-2013-are-very -slow-to-compile-and-generate-huge-object-file .

+7
c ++ lambda c ++ 11 visual-c ++ compiler-bug
source share
2 answers

Not sure how useful such deeply embedded lambdas are, but for what it's worth, as far as I can tell, this is an error, the Visual Studio compiler restricts the document (focus):

The C ++ standard recommends restrictions for various language constructs. The following is a list of constructs in which the Visual C ++ Compiler does not implement the recommended restrictions. the first number is the recommended limit, and the second number is the limit implemented by Visual C ++ :

and includes the following brand:

The nesting levels of compound operators , iteration control structures, and selection control structures [256] (256) .

If we look at the grammar in the draft C ++ standard, the compound statement will eventually return to the primary expression, which includes the lambda expression. Therefore, Visual Studio must support up to 256 levels of nesting.

You can also see this by looking at the grammar for the lambda expression, which looks like this:

lambda-entercer lambda-declarator opt compound statement

The draft draft standard has a set of recommended limitations in Appendix B, but they are only recommendations and do not determine compliance.

Refresh

The bug report updated by OP was updated recently to indicate that this will be fixed in a future version.

+2
source share

Compiling your second exam gives:

 $ time cl x.cpp Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved. x.cpp Microsoft (R) Incremental Linker Version 12.00.21005.1 Copyright (C) Microsoft Corporation. All rights reserved. /out:x.exe x.obj real 0m0.764s user 0m0.000s sys 0m0.140s 

The size

 $ ls -lh x.exe -rwxrwxrwx 1 lt None 118K 14. Jan 16:33 x.exe 

I do not see any problems.

0
source share

All Articles