capture by value class members

后端 未结 3 1762
温柔的废话
温柔的废话 2021-02-02 08:49

Is there a way, when writing a lambda function within a member function, to capture fields of the enclosing class by value? The default catch-all = doesn\'t work be

3条回答
  •  太阳男子
    2021-02-02 09:05

    C++ is a language of speed, and one of most important things in design of the language is performance. If standard designers want to implement an idiom a way to capture all variables of a class by value, think of a really huge class and say me do you want to capture them all by value? even to capture all variables that declared in a function(including this) there is 2 way:

    • by value: variables that defined in a function, leave in stack so they can't be multi megabytes but classes that use heap like vector can still be huge. So take care for using this!

    • by reference: that actually save stack pointer.

    So you see there is no efficient way to capture all variables of a class with value, but you can make a local copy of that class in your function and then capture it by reference!

提交回复
热议问题