I'd like to pass a lambda without using a function template here, but with std::function
or a typed function pointer.
#include <iostream>
#include <list>
#include <functional>
template<typename T>
std::list<T> mapll(std::function<T(T)> f, std::list<T> l) {
for(auto it = l.begin(); it != l.end(); ++it) {
*it = f(*it);
}
}
int main() {
// Make it
std::list<int> l = std::list<int> {1,2,3};
// Map it
mapll([](int n) { return n * 2; }, l);
// Print it
std::cout << "{";
for (auto it = l.begin(); it != l.end(); ++it) {
if (it == l.begin())
std::cout << "," << *it;
std::cout << *it;
}
}
But I'm not succeeding in calling mapll
. The error is:
clang++ -g -DNDEBUG -std=c++17 -Wno-switch-bool -lc main.cpp -o main
main.cpp:51:2: error: no matching function for call to 'mapll'
mapll([](int n) { return n * 2; }, l);
^~~~~ main.cpp:42:14: note: candidate template ignored: could not match
'function<type-parameter-0-0 (type-parameter-0-0)>' against
'(lambda at main.cpp:51:8)' std::list<T> mapll(std::function<T(T)> f, std::list<T> l) {