2

I have a template function taking a function pointer as an argument, and a normal function like so:

template <void()>
int foo() {static int c = 0; return ++c;}
void bar() {}

If I understand correctly, two different function pointers can have different binary representations, even if using them would call the same function. Does this also apply to when they are used as template arguments?

Will passing "pointers to bar" (obtained at different times from different places, but calling the function pointer would call bar) to foo always call the same instantiation of foo?

Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
  • 2
    "If I understand correctly, two different function pointers can have different binary representations, even if using them would call the same function." Where'd you read that? – Sebastian Redl Jun 16 '15 at 09:42
  • @sebastian redl this site, actually. It was mentioned that not all the bits might be used for the function's address, or the compiler might use more than one way to store a function pointer. This was a reason given for why you can't reliably use relational comparisons on the binary representation of function pointers. – Weak to Enuma Elish Jun 16 '15 at 16:28
  • @Sebastian redl I found the answer I read it from. It was describing some reasons why copying a function pointer into a char array and comparing the bytes would give false negatives. http://stackoverflow.com/a/1328635/4756309 – Weak to Enuma Elish Jun 16 '15 at 16:58
  • That answer is about member function pointers, which have a very different representation from normal function pointers. With normal function pointers, I'd expect all to be bitwise equal, although C++ still doesn't give you such a guarantee - it just guarantees that a normal comparison will return true. (OTOH your question can be just as easily be asked about templates with a member function pointer non-type template parameter.) – Sebastian Redl Jun 17 '15 at 07:04

1 Answers1

1

Any two pointers to bar function shall be equals. Extract from C++ 11 Specification Draft : 5.10 2 ... Two pointers compare equal if they ... both point to the same function.

So I cannot imagine any reason why passing different pointers to bar could create different instantiations of foo because all those pointers are equals per specification.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252