Übergeben Sie eine Funktion als expliziten Vorlagenparameter
Im folgenden Codebeispiel der Aufruf vonfoo
funktioniert, während der anruf nachbar
scheitert.
Wenn ich den Anruf auskommentierebar
Der Code kompiliert, was mir die Definition vonbar
selbst ist in Ordnung. Also wie würdebar
richtig angerufen werden?
#include <iostream>
using namespace std;
int multiply(int x, int y)
{
return x * y;
}
template <class F>
void foo(int x, int y, F f)
{
cout << f(x, y) << endl;
}
template <class F>
void bar(int x, int y)
{
cout << F(x, y) << endl;
}
int main()
{
foo(3, 4, multiply); // works
bar<multiply>(3, 4); // fails
return 0;
}