Question

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

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

And the following code (one more nesting level is added) will cause a C1128 error.

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

Also, they are very slow to compile. Is there any explanation for this? I'm using Visual C++ 2013.

Update

This seems to be a bug in Visual C++, I've 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.

Was it helpful?

Solution

Not sure how useful such deeply nested lambdas are but for what it is worth as far as I can tell this is a bug, the Visual Studio compiler limits document states (emphasis mine):

The C++ standard recommends limits for various language constructs. The following is a list of constructs where the Visual C++ compiler does not implement the recommended limits. The first number is the recommended limit and the second number is the limit implemented by Visual C++:

and includes the following bullet:

Nesting levels of compound statements, iteration control structures, and selection control structures [256] (256).

If we look at the grammar in the C++ draft standard compound-statement will eventually get back to primary-expression which includes lambda-expression. So Visual Studio should support up to 256 levels of nesting.

You could also see this by looking at the grammar for lambda-expression which is as follows:

lambda-introducer lambda-declaratoropt compound-statement

The draft standard has a set of recommend limits in Annex B but they are only guidelines and do not determine compliance.

Update

The bug report the OP filed was updated recently to indicate this will be fixed in a future release.

OTHER TIPS

Compiling your second examaple 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 is

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

I don't see any problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top