Ignoring the const
problem, you have a sort of chicken-and-egg problem.
It's true that your lambda can be converted to a std::function<std::uint32_t(std::unit32_t)>
.
But it's also true that the lambda isn't a std::function<std::uint32_t(std::unit32_t)>
so the compiler can't deduce A
.
And if the compiler can't deduce A
, can't convert the lambda to std::function<A(T)>
.
You obviously can explicit the correct std::function
type calling map()
a.map(std::function<std::uint32_t(std::uint32_t)>{[]( uint32_t c ) -> uint32_t {
return c * c;
}});
and, taking in count that you're using C++17 (so you can use the deduction guides for std::function
) also deducing the template parameters for std::function
a.map(std::function{[]( uint32_t c ) -> uint32_t {
return c * c;
}});
but, using again the template deduction guides for std::function
, what about writing mat()
to accept a simply callable and deducing A
from it?
I mean... what about something as follows?
template <typename F>
auto map( F && func ) const {
using A = typename decltype(std::function{std::forward<F>(func)})::result_type;
auto sequence = new Sequence< A >;
for ( const T& element : *this ) {
sequence->push( std::forward<F>(func)( element ) );
}
return *sequence;
}
(caution: code not tested).
You can also deduce A
, without std::function
deduction guides (so before C++17), as suggested by Michael Kenzel.