가변 개수의 인자를 가진 제공된 함수를 호출하는 함수를 만들고 있습니다. Visual Studio 2015에서 올바르게 컴파일되고 작동하지만 Clang에서는 컴파일되지 않습니다. 나는 내가하려고하는 것을 보여주는 시위를 준비했다. 내가 연타 얻을 오류 : 모든 standard library algorithm function 보면, 적어도 사람이 "조건"(호출 가능한 객체)를 복용템플릿 매개 변수 팩은 Clang하지만 실패하지는 않습니다. VS 2015
prog.cpp: In function 'int main()': prog.cpp:31:2: error: no matching function for call to 'run(std::vector&, void ()(int&, const int&), const int&)' ); ^prog.cpp:7:6: note: candidate: template void run(std::vector&, const std::function&, mutrArgs ...) void run( ^prog.cpp:7:6: note: template argument deduction/substitution failed: prog.cpp:31:2: note: mismatched types 'const std::function' and 'void ()(int&, const int&)' );
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
template<int RepeatTimes, class ... mutrArgs>
void run(
vector<int>& vec,
const function<void(int&, mutrArgs ...)>& mutr,
mutrArgs ... args
)
{
for (int times{0} ; times < RepeatTimes ; ++times)
for (auto& item : vec)
mutr(item, args...);
}
void adder(int& i, const int& val)
{
i += val;
}
int main()
{
vector<int> v{0,1,2,3,4,5,6,7,8,9};
const int addValue{4};
run<2, const int&>(
v,
&adder,
addValue
);
for (auto i : v)
cout << i << " ";
cout << endl;
return 0;
}
'std :: function' 대신에 모든 [표준 알고리즘 함수] (http://en.cppreference.com/w/cpp/algorithm)와 같이 형식을 템플릿으로 만드십시오. –
비 데모 기능에서는 설정된 수의 인수를 취해야합니다. – JadziaMD
'std :: function (& adder)'를 사용하면 컴파일됩니다. 왜 그것이 지정해야하는지 확실하지 않습니다. 짐작할 수없는 꾸러미 부분과 관련이 있을지 모르지만 규칙에 대해서는 확실하지 않습니다. –
NathanOliver