Is it safe to assume that two function pointers, that point to different instances of one templated function, will compare unequal?
Even if the templated function does not use the template parameter(s) at all and thus does the exact same thing in every case?
For example the following works fine on my compiler but I’m not sure if it does on any other:
class TypeChecker
{
public:
template< typename T > static void foo( void )
{}
template< typename T > static void setType( void )
{ s_fooPtr = &foo< T >; }
template< typename T > static bool checkType( void )
{ return ( s_fooPtr == &foo< T > ); }
private:
static void ( * s_fooPtr )( void );
};
void ( * TypeChecker::s_fooPtr )( void ) = 0;
int main( void )
{
TypeChecker::setType< char >();
TypeChecker::checkType< char >(); // true
TypeChecker::checkType< unsigned char >(); // false
TypeChecker::checkType< signed char >(); // false
}
When will two pointer compare equal?
According to 5.10/1:
Are
foo<int>()andfoo<char>()the same function?According to 14.4/1:
So apparently
foo<int>()andfoo<char>()are not the same function.So
&foo<int>()and&foo<char>()should not compare equal, whatever optimization is made.EDIT:
As mentioned by @SergeDundich in the comment, 14.4/1 used
ifinsteadif and only if, which gives no guarantee whetherfoo<int>()andfoo<char>()are the same function or not. In other parts of the specification,if and only ifis used a lot.I didn’t find any clarification to this in the specification. However, in the examples, I can find this:
EDIT2:
ifis used instead ofif and only ifas this situation exists: (Example from 14.5.7/2)Vec<int>andvector<int, Alloc<int>>have a lot of differences, but still the same type.However, as to the case of
foo<int>()andfoo<char>(), their signatures are different. The different signatures should render them different functions.Thanks to @JohannesSchaub-litb .