1
我只是試圖輸出所有傳遞給可變參數函數模板的參數。我試過這種遞歸方法:輸出所有可變參數模板參數
#include <iostream>
template<typename TFirst, typename... Arguments>
void Output(TFirst first, Arguments... parameters)
{
std::cout << first << std::endl;
Output(parameters);
}
template<typename TFirst>
void Output(TFirst first)
{
std::cout << first << std::endl;
}
void Output()
{
// do nothing
}
int main()
{
Output(1.0f, 2);
}
但我得到parameter packs not expanded with '...'
。任何人都可以解釋這裏有什麼問題?
--------- ------------編輯
這裏是解決方案:
#include <iostream>
template<typename T>
inline void showArguments(T&& arg) { std::cout << arg << std::endl; }
template<typename First, typename... Rest>
inline void showArguments(First&& first, Rest&& ...rest)
{
showArguments(first);
showArguments(rest...);
}
int main()
{
showArguments("Hello", "World", 1, 2.3, false, true);
}