How to write the test function correctly, so that it can be passed any function f and g as an argument (or a template parameter - not important), with the possibility of calling them with a different type of argument (in the example int, char and long).
#include <iostream> using namespace std; template <typename T> auto f(T x) -> decltype(x*x) { return x * x; } template <typename T> T g(T x) { return x * x; } template <template <class> class F> void test(F f) { auto a = f(32); auto b = f(' '); auto c = f(2000000000L); cout << a << ' ' << b << ' ' << c << endl; } int main() { test(f); test(g); return 0; } This code does not compile with messages:
prog.cpp:15:47: error: variable or field 'test' declared void template <template <class> class F> void test(F f) ^ prog.cpp:15:49: error: missing template arguments before 'f' template <template <class> class F> void test(F f) ^ prog.cpp: In function 'int main()': prog.cpp:26:8: error: 'test' was not declared in this scope test(f); ^