2013-10-24 442 views
0

我有下面的模板函數,現在我想添加一個函數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

我想你想的是:

template <typename T, typename... Args> 
T testfun(T (&f) (T, Args...), T tail, Args... queue) 
{ 
    return f(tail, queue...); // or any other implementation 
} 
相關問題