I am currently doing some exercises with templated functions. I had the task to write a implementation of the transform algorithm. I did it like the following and it works:
template <class in, class out, class T>
out stransform(in b, in e, out d, T p(const T&)) {
while (b != e)
*d++ = p(*b++);
return d;
}
As with the normal transform I have to call the predicate with an explicit type like
stransform(begin(vec1), end(vec1), back_inserter(vec2), predi<double>);
Now, I stumbled upon the C++11 Lambdas and wanted to call my function like this:
stransform(begin(vec1), end(vec1), back_inserter(vec2), [] (double x) ->double {return x * 10;} );
With that I do get a compiler error that the type cant be deduced. This is the thing I dont understand as I am defining the T type in my lambda actually twice.
I did also check back the original transform function, with which it is working. I then checked the implementation of that one and it is obviously implemented with a template class for the whole function. Is that the correct way for implementing predicates with templates?