Weak to Enuma Elish
Weak to Enuma Elish

Reputation: 4637

C++ Will using a pointer to the same function as a template parameter always lead to the same instantiation?

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?

Upvotes: 2

Views: 90

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148890

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.

Upvotes: 1

Related Questions