I'm trying to understand templates and I have this piece of code:
#include <iostream>
#include <functional>
using namespace std;
template<class T>
bool myCompare(const T& a, const T& b) {
return a>b;
}
template<class T>
bool encCompare(const T& a, const T& b, function<bool(const T&, const T&)> comp) {
return comp(a,b);
}
int main() {
cout<<encCompare(3,4, myCompare);
return 0;
}
I'm getting an error with the following message:
main.cpp:17:21: error: cannot resolve overloaded function 'myCompare' based on conversion to type 'std::function<bool(const int&, const int&)>'
17 | cout<<encCompare(3,4, myCompare);
| ~~~~~~~~~~^~~~~~~~~~~~~~~~
Is there a way to pass myCompare
as a std::function
object to the encCompare
function as an argument?