我想編寫一個函數模板,它可以接受可變數量的模板參數,並打印出類型參數的typeid()。name()。我可以這樣做內部類的模板,使用靜態功能如下:帶有可變參數模板的函數
template<typename...>
struct foo;
template<typename H, typename... T>
struct foo<H, T...> {
static void print() {
std::cout << typeid(H).name() << ", ";
foo<T...>::print();
}
};
template<typename H>
struct foo<H> {
static void print() {
std::cout << typeid(H).name() << "\n";
}
};
int main(void)
{
foo<int, float>::print();
return 0;
}
但是,我不能做到以下幾點:
template<typename H, typename... T>
void print() {
std::cout << typeid(H).name() << ", ";
print<T...>();
}
int main(void)
{
print<int, float>();
return 0;
}
我嘗試添加下面的「基地」的情況:
template<typename H>
void print();
和
void print();
都沒有工作。我如何編寫這樣的函數模板?
是你得到了什麼錯誤訊息? – templatetypedef 2012-01-09 19:14:26
您的班級模板專業化不明確。 – 2012-01-09 19:48:29