2

テンプレート引数として静的メンバー関数ポインターを使用すると、最新の 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
4

2 に答える 2

1

編集: コンパイラのバグのようです: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/STLCCSeries6#c634886322325940618

回避策:

その出力を からに変更decltype(A::f1)した を変更します。そして変更decltype(&A::f1)int(int, int)int (__cdecl *)(int,int)

template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (T_Args...), fnc>

template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (*)(T_Args...), fnc>

作業コード:

struct A
{
    static int f1(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();
    return 0;
}
于 2012-11-15T08:07:25.427 に答える
0

次のようなものを試すことができます

#include <iostream>

using namespace std;

struct A
{
    static int f1(int a, int b)
    {
        return a + b;
    }
};

int f2(int a, int b)
{
    return a + b;
}

template <typename T, T X>
struct wrapper
{
    template <typename... Args>
    static bool value(Args... blargs)
    {
        return X(blargs...) == 3;
    }
};

int main()
{
    bool res;
    res = wrapper<decltype(&A::f1), &A::f1>::value(1,2);
    cout << res << endl;
    return 0;
}

しかし、真剣に、これはとても簡単です:

#include <iostream>

using namespace std;

int main()
{
    bool res;
    res = A::f1(a, b) == 3;
    cout << res << endl;
    return 0;
}
于 2012-11-14T21:18:54.867 に答える