boost lambda weirdness (possible bug?)
Hello,
I'm trying to use boost lambda to create a function
David Sankel: ...
class T { public: char m_c; T( char c ) : m_c( c ) { } virtual T * clone() const=0; virtual ~T(){} }; typedef boost::shared_ptr<T> TPtr; typedef std::vector<TPtr> TVec; using namespace boost::lambda;
This works:
const boost::function< void( T const * const) > f = bind( &TVec::push_back, &tVec, bind( constructor<TPtr>(), bind( &T::clone, _1)));
But this doesn't:
const boost::function< void( const T & ) > g = bind( &TVec::push_back, &tVec, bind( constructor<TPtr>(), bind( &T::clone, boost::cref(_1) )));
Note that cref(_1) isn't valid; the parameters are already taken by reference, _1 should suffice.
The compilation error I'm getting is that it cannot create a new object of type T and that is expected since T is abstract. Why would lambda be making copies instead of passing on the reference?
This is a known Lambda bug; internally, it tries to create the type tuple
AMDG David Sankel wrote:
This works:
const boost::function< void( T const * const) > f = bind( &TVec::push_back, &tVec, bind( constructor<TPtr>(), bind( &T::clone, _1)));
But this doesn't:
const boost::function< void( const T & ) > g = bind( &TVec::push_back, &tVec, bind( constructor<TPtr>(), bind( &T::clone, boost::cref(_1) )));
The compilation error I'm getting is that it cannot create a new object of type T and that is expected since T is abstract. Why would lambda be making copies instead of passing on the reference?
I've attached a simplified version of the problem.
There is no need to wrap _1 in cref. Boost.Lambda will pass the arguments by reference anyway. In Christ, Steven Watanabe
participants (3)
-
David Sankel
-
Peter Dimov
-
Steven Watanabe