17

I would like to make a function with a parameter that accepts either std::greater<int> or std::less<int> as the argument. I'm stuck on the syntax for the parameter, though.

This is the format I tried:

myFunction(int a, int b, bool *comp(int, int)) { … }
…
std::greater<int> bigger;
myFunction(2, 3, bigger);

That doesn't work, though, and I suspect the third parameter is just completely wrong. What should it actually be?

cannot convert std::greater<int> to bool* (*)(int, int)

4
  • 2
    pass std::greater<int>() instead
    – Red Alert
    Mar 28, 2014 at 1:22
  • Thanks. I've updated my question to pass in an instance of greater instead of the class. The error is the same; I just didn't copy my code over carefully the first time. My bad.
    – Qaz
    Mar 28, 2014 at 1:25
  • 1
    By the way, your parameter is a function (converted to a function pointer on sight in that context) taking two ints and returning a bool *.
    – chris
    Mar 28, 2014 at 1:43
  • bigger is a functor while you the function only accepts a function pointer. To accept functor make it of the type of functor class which is greater<int>. But that would be useless as you can only pass greater<int> objects then. Hence best go with templated parameter to accept all kinds of functors and bonus is they also accept function pointers.
    – router
    Nov 25, 2022 at 6:36

3 Answers 3

20

Functions taking a comparator are usually implemented via templates:

template <typename Comparator>
myFunction(int a, int b, Comparator comp) { … }

but you could also use std::function to implement it:

myFunction(int a, int b, std::function<bool (int, int)> ) { … }

The first version exposes code in the header but will usually perform better. As for the second version, you can hide the implementation in the .cpp file, but you would lose some performance due to the impossibility to inline the comparator calls.

8

So the trick here is that std::less and std::greater are actually stateless function objects that can be trivially constructed. But they don't support casting to a function pointer.

The efficient choices are either (A) take the comparator via template argument and implement the code in a header:

template<typename C> void myFunc( int a, int b, C comp )

which means you have to implement it in a header file, or (B) type erase the function object via a std::function< bool(int, int) >:

void myFunc( int a, int b, std::function< bool(int, int) > comp )

which has some costs (maybe significant? Profile!) (heap allocation is avoided via small object optimization for stateless std less/greater, but it tends to cost a virtual function call regardless, which can also block inlining).

Or (C) write some code that lets you take a stateless functor and turn it into a function pointer:

template<typename T>
using Type = T;
template<typename StatelessFunctor>
struct function_ptr_of_stateless_t {
  template<typename R, typename... Args>
  operator Type<R(Args...)>*() const {
    return [](Args... args)->R {
     return StatelessFunctor()(std::forward<Args>(args)...);
    };
  }
};
template<typename StatelessFunctor>
function_ptr_of_stateless_t<StatelessFunctor> as_function_ptr() {
  return {};
}

bool myFunction( int a, int b, bool(*comp)(int, int) ) { return comp(a,b); }
int main() {
  std::cout << myFunction(3,7, as_function_ptr<std::less<int>>() ) << "\n";
}

where the template function as_function_ptr takes the type of your stateless functor and creates a throw away type that lets you cast it to any compatible function pointer type.

This has modestly less overhead than the std::function solution, as a call over a function pointer tends to be faster than over a virtual method, and in addition some compilers (like gcc) are quite decent at inlining function pointers, even from one compilation unit to another.

As a bonus, in C++14 you could use:

int main() {
  std::cout << myFunction(3,7, as_function_ptr<std::less<>>() ) << "\n";
}

and it still works pretty optimally.

3

Use a template:

template<class Callable>
myFunction(int a, int b, Callable f);
1
  • The easiest way. I would call it Comparer instead of Callable for clarity. Jan 22, 2022 at 22:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.