http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53097

             Bug #: 53097
           Summary: [c++0x] Missed optimization: lambda closure object
                    could be smaller
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: l...@mit.edu


This code:

include <stdio.h>

int main(int argc, char **argv)
{
  int a, b;
  auto foo = [&](){return a + b;};
  printf("%d\n", (int)sizeof(foo));
  return 0;
}

prints 16 (on x86-64) on gcc 4.6 and something quite close to 4.7 at -O2 and
-Ofast.  This is as expected if the closure object is implemented as imagined
in the spec.

In this particular case, accessing a from the lambda is defined behavior iff
accessing b is defined (because either a and b are both in scope or both out of
scope, so the lambda could be optimized based on the knowledge that a and b are
at a fixed offset from each other.  This would give size 8.

(It sounds like this could be rather difficult.  clang++ 2.9 works the same way
as g++.  I don't really expect to see this optimization implemented anytime
soon.)

Reply via email to