Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why doesn't the compiler optimize this

14 views
Skip to first unread message

Bonita Montero

unread,
Apr 27, 2022, 12:58:36 PM4/27/22
to
#include <iostream>

using namespace std;

int main()
{
int i = 0, j = 0;
auto fn = [&]() { ++i, ++j; };
cout << sizeof fn << endl;
}

g++ 11.1.0, clang 13 and MSVC 2019 print "16", i.e. there are two
pointers to i and j. But that's really not necessary, there should
be only one pointer inside the parent stack frame, i.e. all adressses
should be referred relatively to this pointer.

Andrey Tarasevich

unread,
Apr 27, 2022, 6:32:42 PM4/27/22
to
Obviously, because these compilers still cautiously implement support
for lambda expressions _literally_ as mere syntactic sugar for local
class declarations.

They just don't care/don't dare to optimize the general approach.

Lambda expressions are not seen as nested functions. Nobody even
considered following the "save a pointer to the parent's stack frame"
approach in their implementation. Maybe one day...

--
Best regards,
Andrey Tarasevich


0 new messages