Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the capturing lambda cannot be applied to the std::valarray?

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

like image 335
kreuzerkrieg Avatar asked Sep 03 '17 11:09

kreuzerkrieg


1 Answers

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.

like image 172
StoryTeller - Unslander Monica Avatar answered Nov 15 '22 04:11

StoryTeller - Unslander Monica