When a C++ lambda expression has a lot of captures by reference, the size of the unnamed function object becomes large

后端 未结 3 704
野趣味
野趣味 2020-12-11 18:39

The following code:

int main() {
    int a, b, c, d, e, f, g;
    auto func = [&](){cout << a << b << c << d << e <<          


        
3条回答
  •  甜味超标
    2020-12-11 19:12

    Because that's how it's implemented. I don't know if the standard says anything about how it should be implemented but I guess it's implementation defined how big a lambda object will be in that situation.

    There would be nothing wrong for a compiler to store a single pointer and use the offsets, to do what you suggest, as an optimization. Perhaps some compilers do that, I don't know.

提交回复
热议问题