rnd_nr_gen
rnd_nr_gen

Reputation: 2281

Template argument type deduction won't work within a function object

I have a template function taking another function with templated argument type.

When using it I have to specify the argument type explicitly (1) otherwise it won't compile (2).

template<typename T>
void process(const std::function<void(const T &)>& f)
{
   // ...
}

process<Foo>( [&](const Foo& arg){/*...*/} ); // (1) Ok!

// process( [&](const Foo& arg){/*...*/} ); // (2) Won't Work! 

Is there any trick to let (2) work?

Upvotes: 1

Views: 75

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36513

Just let the compiler figure out the exact type of the whole function :

template<typename T>
void process(T&& f)
{
   // ...
}

Now both your function calls will work.

Upvotes: 5

Related Questions