mardi 30 janvier 2018

Template arguments deduction failed: passing func pointer to std::function [duplicate]

In provided example foo cannot infer type of T for both - function pointer and lambda (commented out), with provided type or foo2 everything works fine. What prevents foo from infering type T? Is there any way to have it done autmatically?

template<typename T>
void foo(std::function<T(const T&)> op) {
    std::cout << op(6) << std::endl;
}

void foo2(std::function<int(const int&)> op) {
    std::cout << op(6) << std::endl;
}

int bar(const int &x) {return 3 * x;}

int main() {
    //    foo(bar);
    //    foo([](const int &x) {return 3 * x;});

    foo<int>(bar);
    foo<int>([](const int &x) {return 3 * x;});

    foo2(bar);
    foo2([](const int &x) {return 3 * x;});

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire