The following code:
int main() { int a, b, c, d, e, f, g; auto func = [&](){cout << a << b << c << d << e << f << g << endl;}; cout << sizeof(func) << endl; return 0; }
outputs 56 compiled with g ++ 4.8.2
Since all local variables are stored in the same stack stack, remembering one pointer is enough to find the addresses of all local variables. Why does a lambda expression build such a large unnamed function object?
I do not understand why you are surprised.
The C ++ standard provides a set of requirements, and each individual implementation can choose any strategy that meets the requirements.
Why does the implementation optimize the size of the lambda object?
, , ?
, ! !, , . , ...
... , :
struct S { int a, b, c, d, e, f, g; }; int main() { S s = {}; auto func = [&](){ std::cout << s.a << s.b << s.c << s.d << s.e << s.f << s.g << "\n"; }; std::cout << sizeof(func) << "\n"; return 0; }
: 4 !
. ( , ).
++ -, , , , .
, , , " " ++-. this, , - bog-standard operator(). " " ++- , " " ..
this
operator()
-, , , . ( ), . , , , , , .
- . , void*, . , , ( undefined). , , ( , , ) lambdas .
void*
. , , . , , . , , .
, .
. , , , , , - .
There would be nothing wrong with compiling one pointer and using offsets to do what you propose as an optimization. Perhaps some compilers do this, I don't know.
Source: https://habr.com/ru/post/1537058/More articles:Android: ListView selects an item and selects the selected item programmatically - androidHow to resize an image in php to put it in a custom size box while maintaining its aspect ratio? - phpChanging the header of a JavaFX control panel - javaApplication Checker - iosUsing Markov chains to generate procedural music - procedural-generationProper use of Laravel program logic to call an API? - phpCustom middleware block throws backtrace exception - ruby | fooobar.comТрассировка iPhone Simulator для ускорения запуска - iosHow a different data type variable inside a printf statement affects each other relatively in print order - cHow to insert data into a parquet table in Hive - hadoopAll Articles