Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda capture (by value) of array is only copying the pointer?

I'm trying to capture (by value) an entire C-style array. The array seems to decay to a pointer... How do I prevent this so that the whole array is captured?

Code:

#include <iostream>

int main()
{
    char x[1024];

    std::cerr << sizeof(x) << "\n";
    [x = x] // copy
    {
        std::cerr << sizeof(x) << "\n";
    }();
}

This prints:

1024  <<-- yay
8     <<-- oops... not a true copy

It should be noted that this works as I had hoped (1024 for both results):


#include <iostream>
#include <array>

int main()
{
    std::array<char, 1024> x;

    std::cerr << sizeof(x) << "\n";
    [x = x] // copy
    {
        std::cerr << sizeof(x) << "\n";
    }();
}
like image 288
user5406764 Avatar asked Jul 04 '21 18:07

user5406764


People also ask

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.

How do lambda captures work?

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. In the following example, we capture the variable ammo and try to decrement it.

What is capture clause in lambda function in c++?

A capture clause of lambda definition is used to specify which variables are captured and whether they are captured by reference or by value. An empty capture closure [ ], indicates that no variables are used by lambda which means it can only access variables that are local to it.

What does it mean to lambda capture this?

In Short. A lambda defined inside a non-static member function can directly access the members of the current object (or its copy) via an appropriate capture clause. But how the current object can be captured has gone through some changes since C++11.

How do you capture an array in a lambda expression?

Capturing an array in C++ lambda. C++ lambda expressions can capture their surrounding scope variables either by value (=) or by reference(&). You can use the lambda capture clause - square brackets [ ] - to indicate which variables need to be captured and how (value or reference).

What is the difference between a Lambda and a captured variable?

A lambda that captures variables, on the other hand, needs a place to put them. When you create a lambda function that captures variables, you are in essence creating an entirely new kind of object, and the function pointer is just one member of that object; the captured variables are the others.

What happens when you capture a shared pointer with a Lambda?

If you capture a shared pointer with a lambda, that lambda will contain a shared pointer, pointing to the same object as the original. It’s just like manually creating two shared pointers. The copy gets passed around with the lambda, ensuring that there’s an extra reference to that object.

Why do we use lambda expressions instead of function pointers?

As the tasks performed by using function pointers are very small, hence it is not worth writing so many lines of code. Thus, Lambda expressions make it easier to do the same job. A Lambda expression is also called an anonymous function.


Video Answer


1 Answers

[x = x]

This one is equal to

auto x1 = x;

Which is actually decay to a pointer.

Just change your lambda capture to [x] to capture x by value. Also you can capture it by reference with [&x].

like image 82
jdfa Avatar answered Oct 24 '22 11:10

jdfa