Explanation:
CLion and it's standard compiler give me an error that the "candidate template [is] ignored", when I write a lambda as parameter for a generic function that takes a lambda as argument.
This lambda takes a generic type T
and returns another unknown type A
.
The container class that I am writing is supposed to support functional operations like these in Scala or the ones from the Java Stream API.
To be exact:
The map function makes huge problems. It is implemented as a member function in a class called Sequence, which takes a generic parameter T
.
It is supposed to take an element of an already known type T
( actually it iterates through the whole sequence ) and convert it into an unknown type A
.
The implementation itself is not the problem, but I cannot call the function with the lambda syntax I know.
Code:
Sequence.h
template< typename T >
class Sequence {
public:
template< typename A >
auto map( std::function< A( const T ) > function ) const {
auto sequence = new Sequence< A >;
for ( const T& element : *this ) {
sequence->push( function( element ) );
}
return *sequence;
}
}
main.cpp
int main() {
Sequence< uint32_t > a;
a.push( 20 );
a.push( 30 );
a.push( 40 );
a.map( []( uint32_t c ) -> uint32_t {
return c * c;
} );
return 0;
}
As far as I understand a lambda gets initialized, which
takes a parameter of type std::uint32_t
and returns a value of type std::uint32_t
.
The generic parameter A
doesn't seem to get inferred at this point.
Error Stack:
main.cpp:21:7: error: no matching function for call to 'Sequence<unsigned int>::map(main()::<lambda(uint32_t)>)'
} );
Sequence.h:143:10: note: candidate: template<class A> auto Sequence<T>::map(std::function<A(T)>) const [with A = A; T = unsigned int]
auto map( std::function< A( const T ) > function ) const {
note: template argument deduction/substitution failed:
main.cpp:21:7: note: 'main()::<lambda(uint32_t)>' is not derived from 'std::function<A(unsigned int)>'
} );
Thanks in advance!