テンプレート引数として静的メンバー関数ポインターを使用すると、最新の VC++ コンパイラ (2012 年 11 月 CTP) で次のコンパイル エラーが発生します。
error C2027: use of undefined type 'wrapper<int (int,int),int A::f1(int,int)>'
しかし、無料の機能を使用すると、すべて正常に動作します。g++ で同様のバグをいくつか調べました(静的メンバー関数へのポインターは、 g++ のテンプレート引数として「無効」です)が、引数が無効であると明示的に記載されています。静的関数の違いは何ですか?
他のいくつかの関連する理由でコンストラクトがコンパイルされないvoid(*)(void)
ため、関数をキャストしています。<typename T_Ret, typename... T_Args, T_Ret(*)(T_Args...)>
struct A
{
static int f1(int a, int b)
{
return a + b;
}
};
int f2(int a, int b)
{
return a + b;
}
template <typename Sig, void(*fnc)(void)>
struct wrapper;
template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (T_Args...), fnc>
{
static bool apply()
{
// get some ints here
int a = 1;
int b = 2;
typedef T_Ret (fnc_ptr*)(T_Args...);
int res = ( (fnc_ptr)fnc )(a, b);
// do smth with result
res;
return true; // or false
}
};
int main()
{
bool res;
res = wrapper<decltype(A::f1), (void(*)(void))A::f1>::apply(); // error
res = wrapper<decltype(f2), (void(*)(void))f2>::apply(); // compiles ok
return 0;
}
編集:わかりました、問題を decltype に絞り込みました。タイプを明示的に記述すると、すべてが機能します。
res = wrapper<int(int, int), (void(*)(void))A::f1>::apply(); // compiles ok