I'm building a lambda function that requires access to a fair number of variables in the context.
const double defaultAmount = [&]{
/*ToDo*/
}();
I'd rather not use [=] in the list as I don't want lots of value copies to be made.
I'm concerned about program stability if I use [&] since I don't want the lambda to modify the capture set.
Can I pass by const reference? [const &] doesn't work.
Perhaps a good compiler optimises out value copies, so [=] is preferable.
You can create and capture const references explicitly:
int x = 42;
const int& rx = x;
auto l = [&rx]() {
x = 5; // error: 'x' is not captured
rx = 5; // error: assignment of read-only reference 'rx'
};
The capture list is limited in what can be captured; basically by-value or by-reference (named or by default), the this pointer and nothing.
From the cppreference;
capture-list - a comma-separated list of zero or more captures, optionally beginning with a capture-default. Capture list can be passed as follows (see below for the detailed description):
[a,&b] where a is captured by value and b is captured by reference.
[this] captures the this pointer by value
[&] captures all automatic variables odr-used in the body of the lambda by reference
[=] captures all automatic variables odr-used in the body of the lambda by value
[] captures nothing
You could create local const& to all the object you wish to capture and use those in the lambda.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
const int& refa = a;
const int b = [&]() -> int {
//refa = 10; // attempts to modify this fail
return refa;
}();
cout << a << " " << b << endl;
}
The capture could be either for all the references, or an explicit list what is required;
const int b = [&refa]()
Another alternative is not to capture the local variables at all. You then create a lambda that accepts as arguments the variables you need. It may be more effort as the local variable count grows, but you have more control over how the lambda accepts its arguments and is able to use the data.
auto lambda = [](const int& refa /*, ...*/) { */...*/ }
lambda(...);
Sadly the C++11 grammar does not allow for this, so no.
You can capture a constant reference to an object, not an object itself:
A a;
const A& ref_a = a;
const double defaultAmount = [&]{
ref_a.smth();
}();
Related
I have a program as below:
int main()
{
int val = 4;
auto add = [val](int a)->int{
val += 2;
return a+val;
};
cout << add(3) << endl;
cout << val << endl;
return 0;
}
There's a compiling error in Xcode: Cannot assign to a variable captured by copy in a non-mutable lambda.
My question is: if we choose to use the copy (using "=" or value name), can't this value be assigned a new value or changed?
Inside a lambda, captured variables are immutable by default. That doesn't depend on the captured variables or the way they were captured in any way. Rather, the function call operator of the closure type is declared const:
This function call operator or operator template is declared const
(9.3.1) if and only if the lambda-expression’s
parameter-declaration-clause is not followed by mutable.
Therefore, if you want to make the captured variables modifiable inside the body, just change the lambda to
auto add = [val] (int a) mutable -> int {
val += 2;
return a+val;
};
so the const-specifier is removed.
The operator () of a lambda is implicitly const unless the lambda is declared mutable - and you can't modify the data members in a const member function. This happens regardless of the type of the capture.
Just capture it by reference, it will work !!
auto add = [&val](int a) -> int{
//
}
This question already has answers here:
Implicit type in lambda capture
(2 answers)
Closed last year.
Why is the variable 'n' in 2nd usage of std::generate and within lambda capture not preceded with it's data type in below code?
I thought it's important to specify the datatype of all identifiers we use in a c++ code.
#include <algorithm>
#include <iostream>
#include <vector>
int f()
{
static int i;
return ++i;
}
int main()
{
std::vector<int> v(5);
auto print = [&] {
for (std::cout << "v: "; auto iv: v)
std::cout << iv << " ";
std::cout << "\n";
};
std::generate(v.begin(), v.end(), f);
print();
// Initialize with default values 0,1,2,3,4 from a lambda function
// Equivalent to std::iota(v.begin(), v.end(), 0);
std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
print();
}
Why is the variable 'n' in 2nd usage of std::generate and within lambda capture not preceded with it's data type in below code?
It's not preceded with a data type, because the grammar of C++ says that it doesn't need to - nor even allowed to - be preceded by a type name.
A type name isn't needed because the type is deduced from the type of the initialiser expression.
From cppreference:
A capture with an initializer acts as if it declares and explicitly captures a variable declared with type auto, whose declarative region is the body of the lambda expression (that is, it is not in scope within its initializer), [...]
Lambdas used the opportunity of a syntax that was anyhow fresh and new to get some things right and allow a nice and terse syntax. For example lambdas operator() is const and you need to opt-out via mutable instead of the default non-const of member functions.
No auto in this place does not create any issues or ambiguities. The example from cppreference:
int x = 4;
auto y = [&r = x, x = x + 1]()->int
{
r += 2;
return x * x;
}(); // updates ::x to 6 and initializes y to 25.
From the lambda syntax it is clear that &r is a by reference capture initialized by x and x is a by value capture initialized by x + 1. The types can be deduced from the initializers. There would be no gain in requiring to add auto.
In my experience n could have been just declared inside the lambda body with auto or int as datatype. Isnt it?
Yes, but then it would need to be static. This produces the same output in your example:
std::generate(v.begin(), v.end(), [] () mutable {
static int n = 0;
return n++; });
However, the capture can be considered cleaner than the function local static.
I've got a quick test below:
#include<iostream>
using namespace std;
int main(){
int i=2;
auto f=[=]()mutable{++i;};
f();
f();
cout<<i<<endl;
return 0;
}
But the result it still prints "2". Why i is not modified inside a mutable lambda? I'm using clang --std=c++1z.
Thanks!
You use [=] for your capture which means the lambda gets a copy of i. This copy is independent from the i in main.
What you need to do is capture by reference [&] in order to apply the changes to the i in main.
int i=2;
auto f=[=]()mutable{++i;};
f();
f();
std::cout<<i<<std::endl;
this prints 2.
int i=2;
auto f=[&](){++i;};
f();
f();
std::cout<<i<<std::endl;
this prints 4.
int i=2;
auto f=[=]()mutable{++i; std::cout << i << std::endl;};
f();
f();
std::cout<<i<<std::endl;
this prints 3 4 2.
= copies captured data into the lambda.
If mutable the copies can be modified.
& references captured data in the lambda.
Modifying things through references is legal.
[=] is the same as [i], and [&] is the same as [&i] in this context (you can explicitly list captures, or let them be captured implicitly by listing none and using = or &).
[=] means you're capturing i by value. Even the mutable lambda could modify it but it's just a copy inside the lambda, then any modification on it would have nothing to do with the original variable.
You might want capture-by-reference, and then mutable is not needed again. e.g.
auto f = [&i]() {++i;};
Consider the following:
void test( const int &value )
{
auto testConstRefMutableCopy = [value] () mutable {
value = 2; // compile error: Cannot assign to a variable captured by copy in a non-mutable lambda
};
int valueCopy = value;
auto testCopyMutableCopy = [valueCopy] () mutable {
valueCopy = 2; // compiles OK
};
}
Why is the first version a compile error when I've declared the lambda as mutable and captured value by value (which I thought made a copy of it)?
Tested with clang (x86_64-apple-darwin14.3.0), which is where the error message comes from, and Visual C++ (vc120).
[C++11: 5.1.2/14]: An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly captured with a capture that does not include an &. For each entity captured by copy, an unnamed non-static data member is declared in the closure type. The declaration order of these members is unspecified. The type of such a data member is the type of the corresponding captured entity if the entity is not a reference to an object, or the referenced type otherwise. [..]
The type of value inside your lambda is const int, because it was captured by copy from a const int&.
Thus, even though the lambda's call operator function is not const (you marked the lambda mutable), the actual implicit member value is of type const int and cannot be mutated.
Frankly, this seems absurd; I would expect this rule to say that the referenced type loses constness, as it's a copy. The presence or absence of the mutable keyword on the lambda itself (and, thus, the presence or absence of the const keyword on the generated call operator function) should be the only access control here.
In C++14 you can work around this by capturing as [value=value], which uses the same rules as auto and thus drops the const. C++'s great, ain't it?
mutable allows a lambda to modify copy of a non-const parameter captured by copy, but it does not allow it for const parameters.
So this code works (and outputs inside 2 outside 1):
int a = 1;
[a]() mutable {
a = 2; // compiles OK
cout << "inside " << a << "\n";
}();
cout << " outside " << a << "\n";
But if we omit mutable, or make a const int, the compiler gives an error.
In our case, the first lambda gives an error because value is const:
void test( const int &value )
If we make copyValue const:
const int valueCopy = value;
then the same error will occur with the second lambda.
I have a program as below:
int main()
{
int val = 4;
auto add = [val](int a)->int{
val += 2;
return a+val;
};
cout << add(3) << endl;
cout << val << endl;
return 0;
}
There's a compiling error in Xcode: Cannot assign to a variable captured by copy in a non-mutable lambda.
My question is: if we choose to use the copy (using "=" or value name), can't this value be assigned a new value or changed?
Inside a lambda, captured variables are immutable by default. That doesn't depend on the captured variables or the way they were captured in any way. Rather, the function call operator of the closure type is declared const:
This function call operator or operator template is declared const
(9.3.1) if and only if the lambda-expression’s
parameter-declaration-clause is not followed by mutable.
Therefore, if you want to make the captured variables modifiable inside the body, just change the lambda to
auto add = [val] (int a) mutable -> int {
val += 2;
return a+val;
};
so the const-specifier is removed.
The operator () of a lambda is implicitly const unless the lambda is declared mutable - and you can't modify the data members in a const member function. This happens regardless of the type of the capture.
Just capture it by reference, it will work !!
auto add = [&val](int a) -> int{
//
}