gpt4 book ai didi

c++ - 取函数模板重载的地址

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:05 25 4
gpt4 key购买 nike

我的代码依赖于 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++ 提示它无法解析重载函数的地址。

有没有办法在不显式告诉这些重载函数的第一个模板参数的情况下解决这个问题?从理论上讲,这是多余的。此外,我使用此帮助程序类的代码非常通用,因此这里的返回类型 fg 可能不太容易获取。

最佳答案

SFINAE 仅在重载决议期间适用,如果没有函数调用,则不存在重载决议。

关于c++ - 取函数模板重载的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com