mercredi 19 décembre 2018

Overload of template function with template function is never selected

Apologies for jumping here, but my google-foo is not up to snuff for template functions (or function templates?) and their overloads with function templates (or template functions?).

I'm trying to overload a function template with another function template that can take a lambda and I can't figure out why it's not selected

Here's my exact scenario

template< typename buffer_t, typename value_t >
void fill(buffer_t buffer, const value_t value)
{
    std::fill(buffer.begin(), buffer.end(), value);
}

//partial specialization for buffers
template< typename sample_t, stride_t stride=1u >
void fill(Buffer<sample_t,stride> &buffer, const sample_t value)
{
    std::fill(buffer.begin(), buffer.end(), value);
}

//overload for filling a buffer with a callable
template< typename sample_t, stride_t stride=1u >
void fill(Buffer<sample_t,stride> buffer, const std::function<sample_t(size_t,size_t)> filler)
{
    const size_t N = buffer.samples();
    for(size_t i = 0; i < N; ++i) buffer[i] = filler(i, N);
}

And here's my error

error: assigning to 'float' from incompatible type 'const (lambda at )' note: in instantiation of function template specialization 'Smule::Audio::fill, (lambda at )' requested here fill(input, [](size_t i, size_t N){ return (float)std::sin(2.0 * M_PI * 4.0 * (double)i / (double)N); });

Aucun commentaire:

Enregistrer un commentaire