我有下面的模板功能,现在我想添加一个函数testfun,其参数将是每个模板函数及其参数。但我不知道如何定义和实现testfun。任何评论表示赞赏。谢谢!
template<typename T>
T hoo(T x)
{
return x;
}
template<typename T, typename... Args>
T hoo(T first, Args... rest)
{
return first + hoo(rest...);
}
int a = 1, b = 2, c = 3;
int tsum = hoo<int>(a, b);
std::cout << tsum << std::endl;
std::string sa = "a", sb = "b";
std::string tssum = hoo<std::string>(sa, sb);
std::cout << tssum << std::endl;
testfun(hoo, a, b); //testfun looks like this
testfun(hoo, sa, sb); //testfun looks like this
//[Update] Add more request
testfun(hoo, a, b, c); //testfun also support this
答案 0 :(得分:0)
我认为你想要那个:
template <typename T, typename... Args>
T testfun(T (&f) (T, Args...), T tail, Args... queue)
{
return f(tail, queue...); // or any other implementation
}