If you have the following template for filtering list:
template <typename T> inline std::list<T> list_filter(std::list<T> const& a, bool (f)(T)) { std::list<T> output; std::copy_if(a.begin(), a.end(), std::back_inserter(output), f); return output; } Then try to call it with the lambda inside like:
std::list<int> lst = {1,2,3,4,5}; auto filtered = list_filter(lst, [](int el) -> bool { return (el % 2 == 0); }); It will produce the error with no matching function for call to list_filter(..., std::__cxx11::list<int>)::<lambda(int)>)'.
Is there any way to bypass that restriction without extracting the lambda into the separate function? Why C++ doesn't allow this obvious pattern?
copy_ifit needs to have the right signature, and if not will produce a compile time error. You don't really gain anything in that regard.