jeudi 5 mai 2016

Instantiate overloaded function template

I encountered this problem when using std::cref. A minimal example looks like this:

template<typename Fn, typename T>
auto apply(Fn f, const T &t) -> decltype(f(t))
{
    return f(t);
}

int n = 123;
apply(std::cref<int>, n);  // <- compile error: can't infer type `Fn`
apply([](const int &x) { return std::cref(x); }, n);  // ok

I think the problem with the first example is that std::cref<T> has two overloaded versions, one accepting a const T & and the other accepting a std::reference_wrapper<const T>. Is it possible to instantiate a specific version in my case?

Aucun commentaire:

Enregistrer un commentaire