I use a template link to capture a function of type reference-to-function, but it fails when I try to call by the parameter () (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, “Types of Functions Can Decay Into Function Pointers”:
void someFunc(int, double);
template<typename T>
void f1(T param);
template<typename T>
void f2(T& param);
f1(someFunc);
f2(someFunc);
So, I expect that param will be a reference function-type and will call it. What's wrong?
UPDATE: Sounds like a Clang optimizer bug! FIX: Evaluating a parameter value before a call - corrects the situation!
std::cout << param << " - " << (*param)(3,5);
source
share