Reputation: 1795
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";
}();
}
Upvotes: 6
Views: 793
Reputation: 699
[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]
.
Upvotes: 7