Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing by constant reference in the lambda capture list

Tags:

c++

c++11

lambda

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.

like image 751
P45 Imminent Avatar asked Jul 02 '15 08:07

P45 Imminent


People also ask

Are lambda captures const?

By default, variables are captured by const value . This means when the lambda is created, the lambda captures a constant copy of the outer scope variable, which means that the lambda is not allowed to modify them.

Does lambda capture reference by value?

The mutable keyword is used so that the body of the lambda expression can modify its copies of the external variables x and y , which the lambda expression captures by value. Because the lambda expression captures the original variables x and y by value, their values remain 1 after the lambda executes.

What does it mean to lambda capture this?

The lambda is capturing an outside variable. A lambda is a syntax for creating a class. Capturing a variable means that variable is passed to the constructor for that class. A lambda can specify whether it's passed by reference or by value.

What does [=] mean in lambda function?

The [=] you're referring to is part of the capture list for the lambda expression. This tells C++ that the code inside the lambda expression is initialized so that the lambda gets a copy of all the local variables it uses when it's created.


Video Answer


2 Answers

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'
};
like image 151
Anton Savin Avatar answered Sep 21 '22 05:09

Anton Savin


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(...);
like image 39
Niall Avatar answered Sep 23 '22 05:09

Niall