I'm trying to experiment w/ lambdas and std::function
What this program do (supposed to do) is simply print the contents of a container
using a specified pattern, (i'm going to use it for file i/o) but i'ts having some errors
It looks to me like your lambda function type doesn't match the one your print() function expects.
std::function <std::string( User* )> is the type of the lambda std::function <std::string( vector<User>::Iterator )> is the type expected by print given the values of arrs.begin() and arrs.end()
I'd just make the pattern lambda take a vector iterator instead of a pointer to a user.
Maybe JLBorges or Cubbi could explain why this doesn't work.
I've seen this problem before, and the easy solution (which might disappoint you) is this:
test.cc:13:6: note: candidate template ignored: could not match 'function<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > (type-parameter-0-0)>' against '<lambda at test.cc:24:20>'
void print( Iterator first, Iterator last,
^
the "why" here is that template argument deduction never performs type conversion (give or take things like const): the compiler is trying to come up with a type to substitute for "Iterator" in the template parameter list such that the function parameter type "std::function <std::string( Iterator )>" becomes exactly the same type as the type of the argument you're calling it with (the lambda's type), and that can't happen.
Either make functor a template parameter as Catfish did (it's the usual approach)
the version that takes a std::function
brace-init list has no type and does not participate in the deduction, it's a bit of a language hack i guess. There are other ways to exclude arguments from deduction, I couldn't think of another simple one that fits here
Unless the function explicitly takes a std::initializer_list<> as the parameter, a brace-enclosed-initializer-list argument is considered in a non-deduced context.
#include <initializer_list>
template < typename T > void foo( std::initializer_list<T> arg ) {}
template < typename T > void bar( T arg ) {}
int main ()
{
foo( {1} ) ; // fine, {1} is a brace-enclosed-initializer-list;
// foo has an std::initializer_list<> parameter
// T is deduced to be int; call foo( std::initializer_list<int> )
bar( std::initializer_list<int>{1} ) ; // fine; bar( std::initializer_list<int> )
// with T == std::initializer_list<int>
bar( {1} ) ; // *** error, bar does not have an initializer list parameter
// the brace-enclosed-initializer-list {1} is used in a non-deduced context
// note: bar( std::initializer_list<int> ) with T == std::initializer_list<int>
// is not considered for argument deduction
}