Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ boost lambda libraries

What might be the best way to start programming using boost lambda libraries.

like image 634
yesraaj Avatar asked Nov 30 '22 13:11

yesraaj


1 Answers

Remaining within the boundaries of the C++ language and libraries, I would suggest first getting used to programming using STL algorithm function templates, as one the most common use you will have for boost::lambda is to replace functor classes with inlined expressions inlined.

The library documentation itself gives you an up-front example of what it is there for:

for_each(a.begin(), a.end(), std::cout << _1 << ' ');

where std::cout << _1 << ' ' produces a function object that, when called, writes its first argument to the cout stream. This is something you could do with a custom functor class, std::ostream_iterator or an explicit loop, but boost::lambda wins in conciseness and probably clarity -- at least if you are used to the functional programming concepts.

When you (over-)use the STL, you find yourself gravitating towards boost::bind and boost::lambda. It comes in really handy for things like:

std::sort( c.begin(), c.end(), bind(&Foo::x, _1) < bind(&Foo::x, _2) );

Before you get to that point, not so much. So use STL algorithms, write your own functors and then translate them into inline expressions using boost::lambda.

From a professional standpoint, I believe the best way to get started with boost::lambda is to get usage of boost::bind understood and accepted. Use of placeholders in a boost::bind expression looks much less magical than "naked" boost::lambda placeholders and finds easier acceptance during code reviews. Going beyond basic boost::lambda use is quite likely to get you grief from your coworkers unless you are in a bleeding-edge C++ shop.

Try not to go overboard - there are times when and places where a for-loop really is the right solution.

like image 165
Fruny Avatar answered Dec 05 '22 06:12

Fruny