Default:
- Temporary const copies of the bound arguments(i.e (_1 + i), i is bound) are stored in the lambda functor. Storing bound arguments in lambda functions
- (It's a const copy, which obey the concept of pure function, no side effect. think about functional programming, or math's function)
- if it's pointer, it will be copied as => type * const this
- This means that the value of a bound argument is fixed at the time of the creation of the lambda function and remains constant during the lifetime of the lambda function object
Exceptions:
- The programmer can control the storing mechanism with
ref
andcref
wrappers. - Wrapping an argument with
ref
, orcref
, instructs the library to store the argument as a reference, or as a reference to const respectively. ref
can always be replaced withvar
, andcref
withconstant_ref
Array types cannot be copied, they are thus stored as const reference by default.
For some expressions it makes more sense to store the arguments as references.
+= etc. (i += _1)
the streaming operators take their leftmost argument as non-const references.
The left argument of compound assignment operators (+=, *=, etc.) are stored as references to non-const.
If the left argument of << or >> operator is derived from an instantiation of basic_ostream or respectively from basic_istream, the argument is stored as a reference to non-const.
For all other types, the argument is stored as a copy.(including pointers)
In pointer arithmetic expressions, non-const array types are stored as non-const references. This is to prevent pointer arithmetic making non-const arrays const.
If the object argument is unbound:
- the parameter passing mode is always by reference(i.e, if it's const, it will be const type&.
Hence, the argument
a
is not copied in the calls to the two lambda functors below: - A a(0,0);
- bind(&A::set_i, _1, 1)(a); // a.i == 1 , by reference
- bind(&A::set_j, _1, 1)(a); // a.j == 1
- (_1+2)(i) // i is bound by reference, i is unbound, to _1
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.