2014-09-28 2 views
7

उदाहरणअंतिम प्रकार के एक भिन्न टेम्पलेट को वापस कैसे करें?

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

एक variadic टेम्पलेट के अंतिम प्रकार वापस जाने के लिए कैसे के लिए?

+0

संबंधित: http://stackoverflow.com/questions/7661643/how-to-detect-the-first-and- अंतिम-तर्क-में-भिन्न-टेम्पलेट्स- http://stackoverflow.com/questions/18942322/effective-way-to-select-last-parameter-of-variadic- टेम्पलेट – Csq

उत्तर

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

संबंधित मुद्दे