Is it possible to write c++ template/macros to check whether two functions have the same signatures

我们两清 提交于 2019-11-27 08:07:42

问题


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.


回答1:


C++11 Solution

No need to write any template yourself.

You can use decltype along with std::is_same:

if (std::is_same<decltype(foo),decltype(bar)>::value )
{
    std::cout << "foo and bar has same signature" << std::endl;
}

Here decltype returns the type of the expression which is function in this case, and std::is_same compares the two types, and returns true if both are same, else false.


C++03 Solution

In C++03, you don't have decltype, so you can implement overloaded function templates as:

template<typename T>
bool is_same(T,T) { return true; }

template<typename T, typename U>
bool is_same(T,U) { return false; }

Now you can use it as:

if (is_same(foo, bar))
{
    std::cout << "foo and bar has same signature" << std::endl;
}

Now that in this case is_same is a function template, not class template. So it is evaluated at runtime as opposed to compile-time. So this will give error:

int a[is_same(foo,bar) ? 10 : 20]; //error (in Standard C++03)
                                   //the size must be known at compile-time!

However, if you need to know it at compile-time, then you've to work more, and implement the functionality as:

typedef char same[1];
typedef char different[2];

template<typename T>
same& is_same_helper(T,T);  //no need to define it now!

template<typename T, typename U>
different& is_same_helper(T,U); //no definition needed!

#define is_same(x,y)   (sizeof(is_same_helper(x,y)) == sizeof(same))

Now use it as:

if (is_same(foo, bar))
{
    std::cout << "foo and bar has same signature" << std::endl;
}

You can use it at compile-time also. so you can write it:

int a[is_same(foo,bar) ? 10 : 20]; //okay

Hope that helps.




回答2:


What about something like this:

#include <iostream>

void a(int)
{ }

void a2(int)
{ }

void b(float)
{ }

struct true_type
{ enum { value = 1 }; };

struct false_type
{ enum { value = 0 }; };

template <typename T, typename U>
false_type
is_same_sig (T, U)
{ return false_type (); }

template <typename T>
true_type
is_same_sig (T, T)
{ return true_type (); }

int
main ()
{
  std::cout << is_same_sig (a, a2).value
            << is_same_sig (a, b).value
            << "\n";
}



回答3:


Here is another alternative solution that works with C++03:

#include <iostream>

using namespace std;

template<typename F1, typename F2>
bool same_signature(F1 const&, F2 const&)
{
    return false;
}

template<typename F>
bool same_signature(F const&, F const&)
{
    return true;
}

void test1(std::string, int) { }
void test2(std::string, int) { }
void test3(std::string, double) { }

int main()
{
    cout << same_signature(test1, test2);
    cout << same_signature(test1, test3);
}


来源:https://stackoverflow.com/questions/14541519/is-it-possible-to-write-c-template-macros-to-check-whether-two-functions-have

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!