samedi 23 avril 2016

Specifying a single type of a Template function

I have a function:

template<class T, class F>
T sum(std::size_t n, F f){
    T out{};
    for(std::size_t i = 0; i<n; ++i) out += f(i);
    return out;
}

that I would like to be able to call like so:

sum<int>(20, [&](std::size_t i){ return g[i] != y[i]; });

or

sum<double>(35, [&](std::size_t i){ return 2*h[i]/(h[i]-7); });

Is there any way of doing this in c++11?

Aucun commentaire:

Enregistrer un commentaire