dangerous use of boost::bind

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

Gast 128 wrote:
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.
Very nice example, thank you. The question whether pointers to data members should return a const reference by default (as opposed to a value) seems settled.
participants (2)
-
Gast 128
-
Peter Dimov