You can also use the function with std::priority_queue . The difference in what you do is that you pass the function std::sort as the parameter of the function, but you are trying to define the function as the parameter of the queue template. This obviously does not work, because the third argument is a type argument, as the error explains. In addition, you cannot even have a pointer or template template.
If you look at reference , you will find that the queue has a constructor for passing the comparison object. This is where you should pass the function.
There is a difference with std::sort . Sorting is a function, and you can let the compiler output its template arguments, so you don't need to explicitly specify them. A queue is a class template, and class template template arguments cannot be output (not in this context, at least).
The template argument defaults to std::less<typename Container::value_type> , but you do not want to use it. Therefore, you must explicitly specify the type of comparison object. You indicate where you are currently trying to pass the object. How to get the type of pointer / link function, you may ask. You can do it this way: decltype(&cmp) . If you have an outdated compiler that does not yet support decltype , you need to specify the type without it: bool (&)(const int&, const int&) .
Here is an example of how to create a queue using your function.
std::priority_queue<int, std::vector<int>, decltype(&cmp)> x(cmp);
source share