dimanche 29 janvier 2017

Pass lambda as template function parameter

Why doesn't the following code compile (in C++11 mode)?

#include <vector>

template<typename From, typename To>
void qux(const std::vector<From>&, To (&)(const From&)) { }

struct T { };

void foo(const std::vector<T>& ts) {
    auto bar = qux(ts, [](const T&) { return 42; });
}

The error message is:

prog.cc:9:16: error: no matching function for call to 'qux'
    auto bar = qux(ts, [](const T&) { return 42; });
               ^~~
prog.cc:4:6: note: candidate template ignored: could not match 'To (const From &)' against '(lambda at prog.cc:9:24)'
void qux(const std::vector<From>&, To (&)(const From&)) { }
     ^

But it doesn't explain why it couldn't match the parameter.

Aucun commentaire:

Enregistrer un commentaire