Xarts
Xarts

Reputation: 77

Static member function pointer as template argument

I get this compile error with the latest VC++ compiler (Nov 2012 CTP) when using static member function pointer as template argument:

error C2027: use of undefined type 'wrapper<int (int,int),int A::f1(int,int)>'

But when using free function, everything works ok. I looked up some similar bugs in g++( pointer to static member function is "invalid" as a template argument for g++ ), but there it explicitly states that argument is invalid. What is so different about static functions?

I'm casting the function to void(*)(void) because construct like <typename T_Ret, typename... T_Args, T_Ret(*)(T_Args...)> don't compile for some other urelated reasons.

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

EDIT: Ok, I narrowed the issue to decltype. When I write the type explicitly, everything works:

res = wrapper<int(int, int), (void(*)(void))A::f1>::apply();  // compiles ok

Upvotes: 2

Views: 2476

Answers (2)

Xarts
Xarts

Reputation: 77

EDIT: Looks like it's a compiler bug: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/STLCCSeries6#c634886322325940618

Workaround:

Change decltype(A::f1) to decltype(&A::f1) which changed its output from int(int, int) to int (__cdecl *)(int,int). And change

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

to

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

Working code:

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

Upvotes: 1

Wug
Wug

Reputation: 13196

You could try something like this:

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

But seriously, this is so much easier:

#include <iostream>

using namespace std;

int main()
{
    bool res;
    res = A::f1(a, b) == 3;
    cout << res << endl;
    return 0;
}

Upvotes: 1

Related Questions