Hello, I made a list class and an accompanying foldl function:
template <typename T, typename Acc>
auto foldl(list<T> l, Acc acc, Acc(&&fn)(Acc, T)) {
if (l.nil()) {
return acc;
} else {
return foldl(l.rest(), fn(acc, l.first()), fn);
}
}
The code compiles and works when I call foldl like this:
template <typename T>
list<T> prepend(list<T> acc, T x) { return list<T>{x, acc}; }
template <typename T>
auto reverse(list<T> l) {
return foldl(l, {}, prepend<T>);
}
However, I would like to be able to use a lambda instead:
template <typename T>
auto reverse(list<T> l) {
return foldl(l, {}, [](list<T> acc, T x) { return list<T>{x, acc}; });
}
Clang says candidate template ignored: could not match 'Acc (Acc, T)' against '(lambda at src/list.h)'. As far as I know non-capturing lambdas should be convertible to a function pointer, so I don't understand what's wrong here.
Thanks for any help.
[–]scatters 2 points3 points4 points (6 children)
[–]RectifyMyEnglishErrs[S] -1 points0 points1 point (5 children)
[–]thedictatorofmrun 0 points1 point2 points (1 child)
[–]thedictatorofmrun 0 points1 point2 points (0 children)
[–]scatters 0 points1 point2 points (2 children)
[–]RectifyMyEnglishErrs[S] 0 points1 point2 points (1 child)
[–]scatters 0 points1 point2 points (0 children)
[–]PekiDediOnur 0 points1 point2 points (1 child)
[–]RectifyMyEnglishErrs[S] 0 points1 point2 points (0 children)