2014-09-28 30 views
7

例如如何返回可變參數模板的最後一種類型?

template<typename... Ts> 
LastTypeOfTs f(); 

如何返回最後一個類型可變參數模板的?

+0

相關:http://stackoverflow.com/questions/7661643/how-to-detect-the-first-and- the-last-argument-in-the-variadic-templates http://stackoverflow.com/questions/18942322/effective-way-to-select-last-parameter-of-variadic-template – Csq 2014-09-28 19:24:55

回答

9

你可以做一個模板遞歸如下:

template<typename T, typename... Ts> 
struct LastTypeOfTs { 
    typedef typename LastTypeOfTs<Ts...>::type type; 
}; 

template<typename T> 
struct LastTypeOfTs<T> { 
    typedef T type; 
}; 

template<typename... Ts> 
typename LastTypeOfTs<Ts...>::type f() { 
    //... 
} 

LIVE DEMO

相關問題