C ++ link to function failures when calling

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);
}

crash

But according to Scott Meyers book, “Types of Functions 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 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);
+6
source share
1 answer

, Clang! : - !

std::cout << param << " - " << (*param)(3,5);
0

All Articles