What unqualifies capturing lambda from being passed to apply
method of std::valarray
?
consider following code:
int main()
{
std::valarray<int> arr = {1, 2, 3, 4, 5, 6};
auto arr1 = arr.apply([](int val) { return val * 2; }); // compiles
int n = 3;
auto arr2 = arr.apply([n](int val) { return val * n; }); //does not compile
return 0;
}
Live on coliru http://coliru.stacked-crooked.com/a/f0407046699574fc
Tested on https://gcc.godbolt.org/
Neither GCC nor MSVC or CLang would compile the above code
The reason is in the definition of std::valarray::apply
:
valarray<T> apply( T func(T) ) const;
valarray<T> apply( T func(const T&) ) const;
The type of func
in both members is a function type. Which when used as the parameter type of another function decays to a function pointer. Those member functions do not accept general functors. Only pointers to regular functions.
Now, a capture-less lambda has an implicit conversion operator to a function pointer. So the first lambda is converted to a int(*)(int)
which is an address of regular function that can execute the lambdas body.
But lambdas that capture state cannot be converted in such a manner, and as you can see, cannot be passed as an argument to std::valarray::apply
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With