Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 获取函数模板重载的地址_C++_Templates_C++11_Overloading_Function Pointers - Fatal编程技术网

C++ 获取函数模板重载的地址

C++ 获取函数模板重载的地址,c++,templates,c++11,overloading,function-pointers,C++,Templates,C++11,Overloading,Function Pointers,我的代码依赖于SFINAE和默认模板参数。除最后两行注释外,它按预期工作: #include <tuple> #include <iostream> using namespace std; template<typename T> void push(T val){ cout<<val<<endl; } template<typename T> T to(){ T dummy; cin>&

我的代码依赖于SFINAE和默认模板参数。除最后两行注释外,它按预期工作:

#include <tuple>
#include <iostream>
using namespace std;

template<typename T> void push(T val){
    cout<<val<<endl;
}

template<typename T> T to(){
    T dummy;
    cin>>dummy;
    return dummy;
}

template<typename T, T> class function_proxy{
    static_assert(sizeof(T)!=sizeof(T), "Error: function_proxy works with functions (duh)");
};

template<typename Return, typename... Args, Return(*func)(Args...)> class function_proxy<Return(*)(Args...), func>{
    static Return call(Args... args){
        return func(args...);
    }
    template<typename... retrieved> static Return call(retrieved... read){
        return call(read..., to<typename tuple_element<sizeof...(read), tuple<Args...> >::type >());
    }
public:
    template<typename Ret=Return, typename enable_if<!is_void<Ret>::value, int>::type=0>
    static int wrapper(){
        push(call());
        return 1;
    }

    template<typename Ret=Return, typename enable_if<is_void<Ret>::value, int>::type=0>
    static int wrapper(){
        call();
        return 0;
    }
};

int f(int arg){ return arg*3; }
void g(){ cout<<"g does nothing"<<endl; }

int main(){
    //SFINAE works nicely
    function_proxy<decltype(&f), &f>::wrapper();
    function_proxy<decltype(&g), &g>::wrapper();
    //Here it doesn't, even though there should be no ambiguity:
    //function_proxy<decltype(&f), &f>::wrapper;
    //function_proxy<decltype(&g), &g>::wrapper;
}
您可以看到,当我调用模板函数时,SFINAE完成了它的工作。但是,当我试图获取唯一一个不会格式错误的函数的地址时,g++抱怨它无法解析重载函数的地址


有没有办法解决这个问题而不显式地告诉这些重载函数的第一个模板参数?理论上,这是多余的。另外,我使用这个helper类的代码是非常通用的,因此这里的f和g的返回类型可能不太容易获取。

SFINAE只在重载解析期间应用,如果没有函数调用,就没有重载解析。

我明白了。关键是,我认为这绝对是安全的,当你以这种方式接收地址时,使用SFINAE是有意义的。有没有可能在未来的标准中看到这种情况?@LorenzoPistone-谁知道呢。这种类似于自省的SFINAE的使用并不是最初提案的一部分,最初提案的重点是平衡诊断、易于使用的模板和函数调用,以及易于编译器实现类型推断和重载解析。一些其他语言确实有标准的内省功能,也许C++会跟随一天。或者,未来可能会给我们带来更多的快乐。