c++ - how to get rid of lambda syntax -



c++ - how to get rid of lambda syntax -

i need create function in "normal" syntax. how alter it?

template <template <class, class> class container> typename const container<course*, std::allocator<course*> > schedule<container>::getallcourses( ) const { container<course*, std::allocator<course*> > newone; std::for_each(courses.begin(), courses.end(), [&newone](course *c) {course* nc = new course(c->getname(),c->getnumber(), c->getfaculty()); newone.push_back(nc);}); //make container , force every course of study homecoming newone; }

actually, need alter function "for_each" utilize outside class. don't know how it. can help?

a lambda function closure type, implemented unnamed functor.

you keywords, understand how perform "conversion". pretty much rule lambda :

[capture_clause](args) -> return_type { /* lambda_body */ }

is practically (in simplified view - generic lambdas or value/ref captures not explicitly shown here)

struct no_name { no_name(capture_clause) : /* initialize closure */ { } return_type operator()(args) { /* lambda_body */ } };

in case, you'd have create class next :

template <template <class, class> class container> struct lamda_fun { container<course*, std::allocator<course*> > &newone; lamda_fun(container<course*, std::allocator<course*> > &newone) : newone(newone) { } void operator()(course *c) { course* nc = new course(c->getname(),c->getnumber(), c->getfaculty()); newone.push_back(nc); } };

if still want rid of lambda syntax phone call like

std::for_each(courses.begin(), courses.end(), lamda_fun<container>(newone));

even though re-create of functor passed for_each, functor wraps reference right thing done.

an easier way utilize for loop though (yes still exist)

// range based version (auto c : courses) { course* nc = new course(c->getname(), c->getnumber(), c->getfaculty()); newone.push_back(nc); } // traditional version (auto = courses.begin(), ite = courses.end(); != ite; ++it) { auto c = *it; course* nc = new course(c->getname(), c->getnumber(), c->getfaculty()); newone.push_back(nc); }

c++ visual-c++ lambda

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -