template<class Fn, class ...Args>
class func_class<Fn(Args...)> // Do not know what to do here
{
typedef typename result_of<Fn(Args...)>::type mytype;
std::function<mytype(Args...)> func_;
std::tuple<Args...> tuple1;
public:
func_class(Fn&& func_in, Args&& ...args)
{
func_ = func_in;
tuple1 = make_tuple(args...);
}
mytype
exe ()
{
mytype ret;
ret = apply(func_, tuple1);
return ret;
}
};
int func(int a) {return a;}
int main () {
// return type of "func" can be deduced by "result_of<decltype(func)&(int)>::type"
// And result_of is declared as "result_of<Fn(Args...)>"
// Want func_class to have the same interface
func_class<decltype(func)&(int)> fc; // Want to declare a object like this
fc.exe();
}代码如下所示。func的返回类型可以由result_of<decltype(func)&(int)>::type推导出来。result_of被声明为result_of<Fn(Args...)>。希望func_class具有与result_of相同的接口。
编译器抱怨如下:
test.cpp:211:7: error: 'func_class' is not a class template我能做什么?提前谢谢你。
发布于 2020-12-17 20:50:58
template<class Sig>
struct bob;
template<class R, class...Args>
struct bob<R(Args...)>{
//...
};专业化。
无法匹配模式的bob的使用将导致编译时错误。
注意,使用R(Args...)语法时,当R是而不是时,返回值和Args...是非参数将导致意外的异常,因为函数参数和返回值类型是如何被C/C++语言修改的。
这就是为什么std::result_of<F(Args...)>被废弃并被std::invoke_result<F, Args...>所取代的原因。
R(Args...)在std::function中被恰当地使用,因为Args...是函数参数,而R是对function<A(Args...)>::operator()的实际返回值。
https://stackoverflow.com/questions/65348085
复制相似问题