4

2つの関数が同じシグネチャ(戻り値の型と引数のリスト)を持っているかどうかを確認するためにc ++テンプレート/マクロを作成することは可能ですか?

これが私がそれをどのように使いたいかという簡単な例です:

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...
}

それで、それはどういうわけか可能ですか、それとも単なる夢のようなものですか?

PS実際、私はc++2003標準に興味があります。

4

3 に答える 3

2

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);
}
于 2013-01-26T21:14:45.123 に答える