Dear all,
In my work I use the boost libraries quite frequently, especially boost::bind to create function objects. However its use is not as transparent as I hoped for. Especially using functions in boost bind with signatures returning const& can give unpredictable results.
For example:
struct KX
{
KX (int i);
//etc.
};
typedef std::pair pair_t;
pair_t do_kx_pair(int i)
{
return std::make_pair(i, KX(i));
}
//following will go wrong:
KX x = boost::bind(&pair_t::second, boost::bind(&do_kx_pair, i))();
The last line compiles on Visual .NET 2003 without any warning (even on level 4). However after spending two days in boost bind source code, I could see that it actually returns const& to a temporary created by boost bind. The reason for this is that arguments are copied by value, and thus functions with const & (or in this case pointer to member) can return then const & from temporaries.
Is there no way that at least boost::bind could issue a compiler warning? Because now I have to think deep before I create a nested boost bind construction. For example the following construction is not wrong; the const & is again taken from a temporary which boost::bind holds, but this is destroyed after the expression:
KX x = boost::bind(&pair_t::second, do_kx_pair(i))();
If this is not solvable, perhaps it's a good idea to include this case in the documentation.
Wkr,
me
(btw boost::bind is a great improvement over the std::mem_fun and std::bind1st etc.)