簡體   English   中英

靜態成員函數指針作為模板參數

[英]Static member function pointer as template argument

使用靜態成員函數指針作為模板參數時,最新的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

編輯:看來這是一個編譯器錯誤: 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;
}

你可以嘗試像這樣

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM