0

Why does the following code (available on godbolt.org) fail with the compiler error:
mismatched types 'std::function<bool(const Target&)>' and 'bool (*)(const TargetA&)'

#include <iostream>
#include <functional>

template <typename Target>
void consume(std::string name, std::function<bool(const Target&)> predicate) {
    std::cout << "test: " << typeid(predicate).name() << std::endl;
}

struct TargetA {};

bool f(const TargetA&) { return false; };

int main() {
    auto predicate = [](const TargetA&) { return false; };

#if 1  // template matching fails: mismatched types 'std::function<bool(const Target&)>' and 'bool (*)(const TargetA&)'
    consume("A", f);
    consume("A", predicate);
#else  // this works
    consume<TargetA>("A", f);
    consume<TargetA>("A", predicate);
#endif
}

Seemingly, the types perfectly match. Explicitly assigning to a std::function variable works!

Robert
  • 719
  • 5
  • 5

0 Answers0