ファンクターをパラメーターとして受け入れ、後で呼び出すことができるテンプレート関数を作成しようとしていました。プログラムは次のとおりです。
#include <iostream>
#include <functional>
using namespace std;
template<typename R, typename... Args>
R call(function<R(Args...)> fun, Args... args)
{
cout << "call@ " << __LINE__ <<endl;
return fun(args...);
}
int main()
{
cout << call(std::plus<int>(),1,2) <<endl;
return 0;
}
G++ は次のように不満を述べています。
g++ -c -Wall -std=c++0x -I../include a.cpp -o a.o
a.cpp: In function ‘int main()’:
a.cpp:16:38: error: no matching function for call to ‘call(std::plus<int>, int, int)’
a.cpp:16:38: note: candidate is:
a.cpp:7:3: note: template<class R, class ... Args> R call(std::function<_Res(_ArgTypes ...)>, Args ...)
a.cpp:7:3: note: template argument deduction/substitution failed:
a.cpp:16:38: note: ‘std::plus<int>’ is not derived from ‘std::function<_Res(_ArgTypes ...)>’
make: *** [a.o] Error 1
と推測std::plus<int>()
できると思いますがstd::function<int(int,int)>
、そうではありませんでした。それはなぜですか?GCCはgcc version 4.7.2 20120921 (Red Hat 4.7.2-2) (GCC)