Domanda

I have below template function, and now I want to add a function testfun whose parameter will be each template function and its parameters. but I do not know how to define and implement testfun. Any comments is appreciated. Thanks!

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
È stato utile?

Soluzione

I assume you want that:

template <typename T, typename... Args>
T testfun(T (&f) (T, Args...), T tail, Args... queue)
{
    return f(tail, queue...); // or any other implementation
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top