Reputation: 6137
for example this works:
if ( typeid( int) == typeid( int) ) //...
how to do the same with function signatures?
if (typeid (void (*)(void) ) == typeid( void(*)(void) ) //that of course dosn't work
how do we check thos two for signature?
void f(int);
int x(double);
Upvotes: 1
Views: 460
Reputation: 8587
Use typeid(foo).name()
.
For instance : if ( typeid(func1).name() == typeid(func2).name() )
//do stuff
#include <cstdio>
#include <iostream>
#include <typeinfo>
using namespace std ;
void foo()
{
}
int bar()
{
return 1;
}
int main(void)
{
if (typeid(foo).name() == typeid(bar).name())
cout<<typeid(foo).name()<<" equals "<<typeid(bar).name()<<" \n";
else
if (typeid(foo).name() != typeid(bar).name())
cout<<typeid(foo).name()<<" is not equal to "<<typeid(bar).name()<<" \n";
cout << "\nPress ENTER to continue \n\n"; cin.ignore(); // pause screen
return 0;
}
output:
void (__cdecl*)(void) is not equal to int (__cdecl*)(void)
Upvotes: 1
Reputation: 355079
The type of a function is known at compile-time. You can compare arbitrary types using is_same
:
#include <iostream>
#include <type_traits>
int main()
{
typedef void(*F0)(int);
typedef void(*F1)(int, int);
std::cout << std::is_same<F0, F0>::value << std::endl;
std::cout << std::is_same<F0, F1>::value << std::endl;
}
Result:
1
0
The type trait value is a compile-time constant and can be used in template instantiation and for SFINAE.
Upvotes: 3