Is it possible to write c++ template/macros to check whether two functions have the same signatures (return type and arguments list) ?
Here’s a simple example of how I want to use it:
int foo(const std::string& s) {...}
int bar(const std::string& s) {...}
if (SAME_SIGNATURES(foo, bar))
{
// do something useful... make Qt signal-slot connection for example...
}
else
{
// signatures mismatch.. report a problem or something...
}
So is it possible somehow or is it just a pipe dream ?
P.S.
Actually I’m interesting in c++ 2003 standard.
C++11 Solution
No need to write any template yourself.
You can use
decltypealong withstd::is_same:Here
decltypereturns the type of the expression which is function in this case, andstd::is_samecompares the two types, and returnstrueif both are same, elsefalse.C++03 Solution
In C++03, you don’t have
decltype, so you can implement overloaded function templates as:Now you can use it as:
Now that in this case
is_sameis a function template, not class template. So it is evaluated at runtime as opposed to compile-time. So this will give error:However, if you need to know it at compile-time, then you’ve to work more, and implement the functionality as:
Now use it as:
You can use it at compile-time also. so you can write it:
Hope that helps.