この例では、一般的な可変個引数テンプレートと関数を使用しています。に渡された引数を出力したいf
:
#include <iostream>
template <typename T>
void print(T t)
{
std::cout << t << std::endl;
}
template <typename...T>
void f(T &&...args)
{
print(args...);
f(args...);
}
int main()
{
f(2, 1, 4, 3, 5);
}
しかし、次のエラーが発生します。
Compilation finished with errors:<br>
source.cpp: In instantiation of '`void f(T ...)` [with `T = {int, int, int, int, int}`]':<br>
source.cpp:16:20: required from here <br>
source.cpp:10:4: error: no matching function for call to '`print(int&, int&, int&, int&, int&)`'<br>
source.cpp:10:4: note: candidate is:<br>
source.cpp:4:6: note: `template<class T> void print(T)`<br>
source.cpp:4:6: note: template argument deduction/substitution failed:
source.cpp:10:4: note: candidate expects 1 argument, 5 provided
実際、可変個引数関数を使用するのはこれが初めてであり、それらの使用方法を正確に理解していません。
また、なぜこれが機能しないのか、それを支援するために何ができるのかわかりません。