[英]in C++, I would like to define a class object from a template, by using a typename information coming from a variable(e.g. string)
[英]How do I define a class template with specfic format of taking template arguments? e.g.: Fn(Args…)
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
我能做些什么? 先感谢您。
template<class Sig>
struct bob;
template<class R, class...Args>
struct bob<R(Args...)>{
//...
};
专业化。
使用模式匹配失败的bob
会产生编译时错误。
请注意,当R
不是返回值且Args...
不是arguments 时使用R(Args...)
语法将导致意外的怪癖,因为 ZC1C425268E68385D1AB5074C17A9 如何修改参数的 C/C1C425268E68385D1AB5074C17A9语。
这就是为什么不推荐使用std::result_of<F(Args...)>
并用std::invoke_result<F, Args...>
代替的原因。
R(Args...)
is appropriately used in std::function
, because Args...
are function arguments, and R
is an actual return value, to function<A(Args...)>::operator()
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.