I use a templated reference to capture a function by reference-to-function-type but it crashes when I try to call by param() (with Apple LLVM version 9.0.0 (clang-900.0.38), x86_64-apple-darwin17.2.0)
#include <iostream>
#include <typeinfo>
int doit(int a, int b) {
return a+b;
}
template <typename T>
void test(T & param) {
std::cout << typeid(T).name() << " ";
std::cout << typeid(param).name() << " ";
std::cout << param(3,5);
}
int main()
{
test(doit);
}
But according to Scott Meyers book "Function types can decay into function pointers":
void someFunc(int, double); // someFunc is a function; type is void(int, double)
template<typename T>
void f1(T param); // in f1, param passed by value
template<typename T>
void f2(T& param); // in f2, param passed by ref
f1(someFunc); // param deduced as ptr-to-func; type is void (*)(int, double)
f2(someFunc); // param deduced as ref-to-func; type is void (&)(int, double)
So I expect the param to be a reference-to-function-type and call by it. Whats wrong?
UPDATE: Looks like this is a Clang optimizer bug! FIX: Evaluation of param's value BEFORE calling by it - fixes the situation!
std::cout << param << " - " << (*param)(3,5);
