I'm working on a function which invokes a supplied function with a variable number of arguments. It compiles and works correctly on Visual Studio 2015, but fails to compile on Clang . I've prepared a demonstration which shows what I'm trying to do. The error I get in Clang is:
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, make the type a template too, like all standard algorithm functions.std::function<void(int&,const int&)>(&adder)it compiles. Not sure why it needs to be specified though. Might have to do with the pack part not being deducible but I am uncertain of the rules.run<2, const int&>, it works too Demostd::function<decltype(adder)>(adder)compile with clang++ 3.5 and with g++ 4.9.2