C++ c++;11:使用向量的元素调用变量函数,并自动推断参数数
尝试展开此处的示例: 基本上,我想从自动传入的函数中推断参数的数量(这显然不适用于重载函数)。我希望这也适用于函子和lambda 无法编译对C++ c++;11:使用向量的元素调用变量函数,并自动推断参数数,c++,vector,call,variadic,C++,Vector,Call,Variadic,尝试展开此处的示例: 基本上,我想从自动传入的函数中推断参数的数量(这显然不适用于重载函数)。我希望这也适用于函子和lambda 无法编译对call\u helper的调用:错误:模板参数中的分析错误 我似乎不知道如何传入作为模板参数返回参数数的constexpr 这就是我到目前为止所做的: #包括 模板 constexpr std::size\u t get\u args\u count(R(*f)(t…) { 返回大小…(T); } 模板 结构索引{ typedef索引next; }; 模板
call\u helper
的调用:错误:模板参数中的分析错误
我似乎不知道如何传入作为模板参数返回参数数的constexpr
这就是我到目前为止所做的:
#包括
模板
constexpr std::size\u t get\u args\u count(R(*f)(t…)
{
返回大小…(T);
}
模板<标准::大小\u t。。。Ns>
结构索引{
typedef索引next;
};
模板
结构生成索引{
typedef typename生成索引::type::next type;
};
模板
结构生成索引<0>{
类型定义索引类型;
};
无效abc(int){}
void abc2(int,int){}
//帮助函数,因为我们需要一种方法
//推导指数包
模板
void call_helper2(函数f,常量std::向量和参数,索引)
{
f(args[Is]…);//展开索引包
}
模板
void call_helper(函数f、常量std::向量和参数)
{
调用_helper2(f,args,typename make_index::type());
}
模板
无效调用(Func f,const std::vector和args)
{
if(args.size()
我错过了什么或做错了什么?感谢您的帮助
编辑:添加了functor和lambda用例有两个错误:
call\u helper(f,args)
中,get\u args\u count(decltype(f))
毫无意义,因为get\u args\u count
接受函数指针而不是类型(显然)李>
f
的参数数量,然后才能使其成为可在constexpr
上下文中使用的表达式之外的其他表达式
#include <vector>
template< std::size_t... Ns >
struct indices {
typedef indices< Ns..., sizeof...( Ns ) > next;
};
template< std::size_t N >
struct make_indices {
typedef typename make_indices< N - 1 >::type::next type;
};
template<>
struct make_indices< 0 > {
typedef indices<> type;
};
void abc(int) {}
void abc2(int, int) {}
// helper function because we need a way
// to deduce indices pack
template<typename Func, size_t... Is>
void call_helper2(Func f, const std::vector<int>& args, indices<Is...>)
{
f( args[Is]... ); // expand the indices pack
}
template<typename Func, size_t N>
void call_helper(Func f, const std::vector<int>& args)
{
call_helper2(f, args, typename make_indices<N>::type());
}
template<class R, class ... T>
void call(R(*f)(T ...), const std::vector<int>& args)
{
if (args.size() < sizeof...(T)) throw 42;
call_helper<
decltype(f), sizeof...(T)
>(f, args);
}
int main()
{
std::vector<int> v(2);
call(&abc2, v);
}
#包括
模板<标准::大小\u t。。。Ns>
结构索引{
typedef索引next;
};
模板
结构生成索引{
typedef typename生成索引::type::next type;
};
模板
结构生成索引<0>{
类型定义索引类型;
};
无效abc(int){}
void abc2(int,int){}
//帮助函数,因为我们需要一种方法
//推导指数包
模板
void call_helper2(函数f,常量std::向量和参数,索引)
{
f(args[Is]…);//展开索引包
}
模板
void call_helper(函数f、常量std::向量和参数)
{
调用_helper2(f,args,typename make_index::type());
}
模板
无效调用(R(*f)(T…,常量std::vector和args)
{
if(args.size()(f,args);
}
int main()
{
std::向量v(2);
呼叫(&abc2,v);
}
YSC的答案解决了常规函数的问题,但它不适用于lambda和函子。为了使这个函数也适用于functor和lambda,我必须添加一个重载call
(基于中的解决方案):
模板
结构索引{
typedef索引next;
};
模板
结构生成索引{
typedef typename生成索引::type::next type;
};
模板
结构生成索引<0>{
类型定义索引类型;
};
无效abc(int){}
void abc2(int,int){}
//帮助函数,因为我们需要一种方法
//推导指数包
模板
void call_helper2(函数f,常量std::向量和参数,索引)
{
f(args[Is]…);//展开索引包
}
模板
void call_helper(函数f、常量std::向量和参数)
{
调用_helper2(f,args,typename make_index::type());
}
模板
无效调用(R(*f)(T…,常量std::vector和args)
{
//此重载用于常规函数
if(args.size()
调用\u helper
?这不会编译,但是我如何使它也能与lambdas一起工作?我还想使用work,例如调用([&](int,int,int){(void)v.empty();},v)代码>不错。请注意,尽管在发布答案后更改问题的范围被认为是次佳选择;我宁愿问一个新问题。你说得对,我道歉。我只是假设它的工作原理是一样的,但不久之后发现它并没有涵盖所有典型的用例。