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 because when I reference the variable inside the lambda I get instead a dereferencing from the captured this pointer, as well as explicitly naming the variable in the capture list, because I get two compile error: capture of non-variable <name>
, and ‘this’ was not captured for this lambda function
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Why doesn't C11 support lambda functions
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
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!
No, data members cannot be captured by value. A lambda can capture only two kinds of things:
this
pointer, andAs has been noted by ildjarn in the comments, you can create a local variable with a copy of the value of the data member, and capture that local variable by value.
I would argue that if explicit by-value capture of a data member was allowed, it could prove confusing, since the behavior of explicit capture would differ from that of implicit capture. For example, given an accessible data member of type
int
namedm
, it would be odd that the following would produce different results:Yes, simply write the
[<new name>=<your class field>]
construct. For instance: