필자는 매개 변수로 펑터를 받아 들일 수있는 템플릿 함수를 작성하고 나중에 호출하려고했습니다.템플릿 함수의 std :: function
#include <iostream>
#include <functional>
using namespace std;
template<typename R, typename... Args>
R call(function<R(Args...)> fun, Args... args)
{
cout << "[email protected] " << __LINE__ <<endl;
return fun(args...);
}
int main()
{
cout << call(std::plus<int>(),1,2) <<endl;
return 0;
}
G ++ compplains : 다음과 같이 프로그램입니다
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)
+1 '__LINE__'의 쓸데없는 점을 놓쳤습니다. _all_ 호출에 _same_ 행을 인쇄합니다. –
나는 강조하기를 원한다 : "** std :: function' **"을 사용할 필요가 없다. 'std :: function'이 과용 될 것 같습니다. –