2010/03/24

The Memo of a Mistake on std::functional

Although I usually write functors by boost::bind or boost::lambda::bind, I wrote these by standard templates in the last weekend. At that time, I had following easy mistake.

std::vector<zzz> xxx;
std::find(xxx.begin(), xxx.end(), std::bind2nd(&zzz.foo, yyy);

I spent some time to find the reason of this compilation error.

If I would write it with boost, I write it like the following:

namespace bl = boost::lambda;
bl::bind(&zzz.foo, bl::_1, yyy);
,
which is the reason of this mistake.

So, I forgot that it is necessary to std::mem_fun.

std::find(xxx.begin(), xxx.end(), std::bind2nd(std::mem_fun(&zzz.foo), yyy);

Programming is often restricted on using third-party's library by something; For example, portability, license, and/or contract. Although for example VC9 has std::tr1 including std::tr1::bind, I am sorry it cannot always be used. In such cases, non-restricted Standard library is very important, which is more troublesome than the Boost just a little now.

No comments:

How to set parameters to debugging program on Visual Studio 2019 with CMake

Solution: MSDN Sometimes the "Debug and Launch Settings for CMake" bottun is disabled. In this case, change to the target view. ...