Ich arbeite an einer Funktion, die eine angegebene Funktion mit einer variablen Anzahl von Argumenten aufruft. Es kompiliert und funktioniert ordnungsgemäß in Visual Studio 2015, kann jedoch nicht mit Clang kompiliert werden. Ich habe eine Demonstration vorbereitet, die zeigt, was ich versuche zu tun. Der Fehler, den ich in Clang bekommen ist:Template Parameter Pack schlägt auf Clang, aber nicht 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;
}
Anstatt "std :: function" zu verwenden, muss auch der Typ eine Vorlage sein, wie alle [standard algorithm functions] (http://en.cppreference.com/w/cpp/algorithm). –
In der Nichtdemonstrationsfunktion ist es erforderlich, eine bestimmte Anzahl von Argumenten zu verwenden. – JadziaMD
Wenn Sie 'std :: function (& addierer)' verwenden, kompiliert es. Nicht sicher, warum es jedoch angegeben werden muss. Könnte damit zu tun haben, dass der Packteil nicht ableitbar ist, aber ich bin mir der Regeln nicht sicher. –
NathanOliver