Visual c++ VS2010 SFINAE和函数重载错误

Visual c++ VS2010 SFINAE和函数重载错误,visual-c++,overloading,sfinae,Visual C++,Overloading,Sfinae,我有这样的代码,它在除VS2010之外的所有我测试过的编译器中编译罚款。 我在这里尽量不使用C++11的特定功能,因此它仍然可以在过时的编译器(如GCC4.1)上编译 #include <iostream> using namespace std; // Simplified variant class struct Var { template <class T> Var(T t) {} Var(void) {} }; // Sim

我有这样的代码,它在除VS2010之外的所有我测试过的编译器中编译罚款。 我在这里尽量不使用C++11的特定功能,因此它仍然可以在过时的编译器(如GCC4.1)上编译

    #include <iostream>

using namespace std;

// Simplified variant class
struct Var
{
    template <class T>
    Var(T t) {}

    Var(void) {}
};
// Simplified argument array class
struct FuncArgs
{

};


/** Make a wrapper around the given function */
template <int line, typename Ret, Ret Func()>
Var WrapFuncT(const FuncArgs & args)
{
    Var ret;
    ret = Func(); return ret;
}
/** Make a wrapper around the given function */
template <int line, void Func()>
Var WrapFuncT(const FuncArgs & args)
{
    Var ret;
    Func(); return ret;
}
// Unary
template <int line, typename Ret, typename Arg1, Ret Func(Arg1)>
Var WrapFuncT(const FuncArgs & args)
{
    Var ret; Arg1 arg;
    ret = Func(arg);                   
    return ret;
}
template <int line, typename Arg1, void Func(Arg1)>
Var WrapFuncT(const FuncArgs & args)
{
    Var ret; Arg1 arg;
    Func(arg);                         
    return ret;
}
// Binary
template <int line, typename Ret, typename Arg1, typename Arg2, Ret Func(Arg1, Arg2)>
Var WrapFuncT(const FuncArgs & args)
{
    Var ret; Arg1 arg1; Arg2 arg2;
    ret = Func(arg1, arg2);                   
    return ret;
}
template <int line, typename Arg1, typename Arg2, void Func(Arg1, Arg2)>
Var WrapFuncT(const FuncArgs & args)
{
    Var ret; Arg1 arg1; Arg2 arg2;
    Func(arg1, arg2);                         
    return ret;
}
#define WrapFunc(X, Y, ...) &WrapFuncT<__LINE__, X, Y, ## __VA_ARGS__ >   

int testFunc()
{
    return 42;
}

void testFunc2(int value)
{
    cout<<value<<endl;
}   

typedef Var (*NamedFunc)(const FuncArgs &);

int main()
{
   NamedFunc a, b;
   a = WrapFunc(int, testFunc);
   b = WrapFunc(int, testFunc2);

}
#包括
使用名称空间std;
//简化变量类
结构变量
{
模板
Var(T){}
Var(void){}
};
//简化参数数组类
结构函数
{
};
/**围绕给定函数制作一个包装器*/
模板
变量WrapFuncT(常量函数和参数)
{
Var-ret;
ret=Func();返回ret;
}
/**围绕给定函数制作一个包装器*/
模板
变量WrapFuncT(常量函数和参数)
{
Var-ret;
Func();返回ret;
}
//一元
模板
变量WrapFuncT(常量函数和参数)
{
变量ret;Arg1 arg;
ret=Func(arg);
返回ret;
}
模板
变量WrapFuncT(常量函数和参数)
{
变量ret;Arg1 arg;
Func(arg);
返回ret;
}
//二进制的
模板
变量WrapFuncT(常量函数和参数)
{
变量ret;Arg1 Arg1;Arg2 Arg2;
ret=Func(arg1,arg2);
返回ret;
}
模板
变量WrapFuncT(常量函数和参数)
{
变量ret;Arg1 Arg1;Arg2 Arg2;
Func(arg1、arg2);
返回ret;
}
#定义WrapFunc(X,Y,…)和WrapFunc
int testFunc()
{
返回42;
}
void testFunc2(int值)
{

cout您可以尝试使用通用模板函数并使用结构专用化,例如:

namespace detail
{

// class to specialize for each function type
template <int line, typename F, F f> struct helper_wrapper;

// partial specialization
template <int line, typename Ret, Ret (&Func)()>
struct helper_wrapper<line, Ret (&)(void), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        ret = Func();
        return ret;
    }
};

// partial specialization
template <int line, void (&Func)()>
struct helper_wrapper<line, void (&)(), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        Func();
        return ret;
    }
};

// partial specialization
template <int line, typename Ret, typename Arg1, Ret (&Func)(Arg1)>
struct helper_wrapper<line, Ret (&)(Arg1), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        Arg1 arg;
        ret = Func(arg);
        return ret;
    }
};

// partial specialization
template <int line, typename Arg1, void (&Func)(Arg1)>
struct helper_wrapper<line, void (&)(Arg1), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        Arg1 arg;
        Func(arg);
        return ret;
    }
};

// other partial specialization omitted. 

}

// The general function
template <int line, typename F, F f>
Var WrapFuncT(const FuncArgs& arg) { return detail::helper_wrapper<line, F, f>()(arg); }

// The helper macro
#define WrapFunc(X, Y)    &WrapFuncT<__LINE__, X, Y>

我不希望在宏中写入函数的签名。此外,此代码在VS2010中仍然不起作用,错误为
错误C2440:typecast无法从“重载函数”转换为Var(*)(const FuncArgs&)< /代码> OK,通过替换对函数指针的引用来修复最后一个错误。VS2010似乎考虑类型<代码> RET(&)(ARG)(而不是<代码> RT(*)(ARG)< /代码>,即使实例化为<代码> FUNC
namespace detail
{

// class to specialize for each function type
template <int line, typename F, F f> struct helper_wrapper;

// partial specialization
template <int line, typename Ret, Ret (&Func)()>
struct helper_wrapper<line, Ret (&)(void), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        ret = Func();
        return ret;
    }
};

// partial specialization
template <int line, void (&Func)()>
struct helper_wrapper<line, void (&)(), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        Func();
        return ret;
    }
};

// partial specialization
template <int line, typename Ret, typename Arg1, Ret (&Func)(Arg1)>
struct helper_wrapper<line, Ret (&)(Arg1), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        Arg1 arg;
        ret = Func(arg);
        return ret;
    }
};

// partial specialization
template <int line, typename Arg1, void (&Func)(Arg1)>
struct helper_wrapper<line, void (&)(Arg1), Func>
{
    Var operator()(const FuncArgs&) const
    {
        Var ret;
        Arg1 arg;
        Func(arg);
        return ret;
    }
};

// other partial specialization omitted. 

}

// The general function
template <int line, typename F, F f>
Var WrapFuncT(const FuncArgs& arg) { return detail::helper_wrapper<line, F, f>()(arg); }

// The helper macro
#define WrapFunc(X, Y)    &WrapFuncT<__LINE__, X, Y>
a = WrapFunc(int(&)(), testFunc);
b = WrapFunc(void(&)(int), testFunc2);