mercredi 15 janvier 2020

C++: Passing a lambda as a function with template arguments makes deducing the template types fail

Using a template type within the parameters of a function passed as argument (either as std::function or as a function pointer) makes the compiler unable to deduce the type when the function is passed as a lambda (see code below). Interestingly this works if the function is not a lambda.

Is there any way to achieve this? I've tried the following code with Visual Studio 2017 and GCC 7.4 and both fail, so it's unlikely to be a compiler bug and probably there is a reason for this behaviour.

#include <iostream>
#include <functional>

template <typename T>
void collide(void (*callback)(T)) // or std::function<void(T)> callback
{
    callback(42);
}

int main() {
    // doesn't compile: "could not deduce template arguments"
    collide([](int a) {
        std::cout << a << std::endl;
    });
}

Aucun commentaire:

Enregistrer un commentaire