0

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?

Enlico
  • 23,259
  • 6
  • 48
  • 102
user2565010
  • 1,876
  • 4
  • 23
  • 37
  • Not really. It does mention that instantiation of the template also fails but I still don't understand why. I also want to know if there's a way to not use pointers to functions and instead use `std::function` – user2565010 Oct 14 '22 at 08:28

0 Answers0