0

I think it is a big limitation of how C++ handles function pointers and std::function, that it is currently impossible to compare two arbitrary functions of different type in an elegant way.

I am now wondering if C++17 will change that with the introduction of std::any

void foo(int a) {}
int bar(float b) {}

void main()
{
    std::any oneFoo = std::make_any(foo);
    std::any oneBar = std::make_any(bar);
    std::any anotherFoo = std::make_any(foo);

    bool shouldBeFalse = (oneBar == oneFoo);
    bool shouldBeTrue = (oneFoo == anotherFoo);
}

Is this gonna behave the way I expect it ?

N0vember
  • 995
  • 1
  • 9
  • 12
  • 4
    [Any reference](http://en.cppreference.com/w/cpp/utility/any) would make it clear that it doesn't have comparison operators. That would limit what you can put into it more than necessary. – chris Apr 13 '17 at 10:49
  • So even with C++17 we would still have no elegant cross platform way to solve this problem ? Damn – N0vember Apr 13 '17 at 10:51
  • Function objects can't even be compared like that themselves normally, let alone when type-erased. – chris Apr 13 '17 at 10:54
  • 2
    Well, there's always `std::function`'s [`target()` member function](http://en.cppreference.com/w/cpp/utility/functional/function/target). Other than that, be aware that function identity is kind of a fuzzy concept in C++. – ComicSansMS Apr 13 '17 at 10:54
  • @ComicSansMS The concept I'm interested in is identity as in same adress, but I read that the way function pointers are implemented in C++ you don't actually have a reliable way to check for that – N0vember Apr 13 '17 at 10:56
  • 1
    So do you want any `int(int)` to match any other `int(int)`? – NathanOliver Apr 13 '17 at 11:34
  • 2
    @N0vember Well, you can do that [as long as they have the same type](http://stackoverflow.com/questions/12898227/comparing-function-pointers). If they don't have the same type you kind of already know the answer beforehand, they're just not equal. – ComicSansMS Apr 13 '17 at 11:40

1 Answers1

1

Comparing function pointers is easy. You are not even required to use C++17, but it helps shorten the code :

#include <iostream>

void foo(int a) {}
int bar(float b) { return 0; }

template <class A, class B> bool pointer_equals(A *a, B *b) {
  if
    constexpr(std::is_same<A, B>::value) { return a == b; }
  else {
    return false;
  }
}

int main(int, char *[]) {
  std::cout << "Should be false: " << pointer_equals(foo, bar) << "\n"
            << "Should be true: " << pointer_equals(foo, foo) << "\n";
  return 0;
}

The problem is : functions are just one kind of callable, and there is a lot of different possible callables in C++. Equality by pointer is kind of weak from a functional standpoint.

Jean-Bernard Jansen
  • 7,544
  • 2
  • 20
  • 17
  • Well, for two arbitrary functions it is undecidable whether they compute the same thing, so what more would you hope for? – Corristo Apr 13 '17 at 16:23