I know there was already Default value on generic predicate as argument.
But maybe there are new options now with c++11, c++14, or c++17?
How can i make this work without overload?
#include <vector> #include <numeric> #include <algorithm> #include <iterator> #include <iostream> #if 0 template <typename CONTAINER, typename PRED> void print(CONTAINER const & cont, PRED pred = [] (typename CONTAINER::value_type) { return true;}) { std::copy_if(cont.begin(), cont.end(), std::ostream_iterator<typename CONTAINER::value_type>(std::cout, " "), pred); std::cout << std::endl; } #else template <typename CONTAINER, typename PRED> void print(CONTAINER const & cont, PRED pred) { std::copy_if(cont.begin(), cont.end(), std::ostream_iterator<typename CONTAINER::value_type>(std::cout, " "), pred); std::cout << std::endl; } template <typename CONTAINER> void print (CONTAINER const & cont) { std::copy(cont.begin(), cont.end(), std::ostream_iterator<typename CONTAINER::value_type>(std::cout, " ")); std::cout << std::endl; } #endif bool even( const int& i) { return not (i % 2); } int main( int argc, char **argv) { std::vector<int> myVec(20); std::iota(myVec.begin(), myVec.end(), 1); print(myVec); print(myVec, even); return 0; } Enabling #if 0 section results in
../main.cpp:17:6: note: template argument deduction/substitution failed:
../main.cpp:56:13: note: couldn't deduce template parameter ‘PRED’
printcall two-parameterprint, and thus avoid duplicating the logic.