I am trying to catch up with C ++ 11 and all the great new features. I got a little stuck on lambdas.
Here is the code I managed to get:
#include <iostream> #include <cstdlib> #include <vector> #include <string> #include <functional> using namespace std; template<typename BaseT, typename Func> vector<BaseT> findMatches(vector<BaseT> search, Func func) { vector<BaseT> tmp; for(auto item : search) { if( func(item) ) { tmp.push_back(item); } } return tmp; } void Lambdas() { vector<int> testv = { 1, 2, 3, 4, 5, 6, 7 }; auto result = findMatches(testv, [] (const int &x) { return x % 2 == 0; }); for(auto i : result) { cout << i << endl; } } int main(int argc, char* argv[]) { Lambdas(); return EXIT_SUCCESS; }
I would like to have the following:
template<typename BaseT> vector<BaseT> findMatches(vector<BaseT> search, function <bool (const BaseT &)> func) { vector<BaseT> tmp; for(auto item : search) { if( func(item) ) { tmp.push_back(item); } } return tmp; }
Basically, I want to narrow down possible lambdas to a reasonable subset of functions. What am I missing? Is it possible? I am using GCC / g ++ 4.6.
c ++ lambda c ++ 11 templates
Mithrandir
source share