boost::lambda std::map

2019-09-02 15:57发布

问题:

I want to simplify my code by using boost::lambda. Here is my code:

// Declare container:
typedef std::map< PageId, Page* > Pages;
Pages m_pages;

// For serialization:
template < class DataType > TPair< DataType > makePair( const std::string& identification, const DataType& dataType )
{
    return TPair< DataType >( identification, dataType );
}

#define SERILIZE_CLASS( _value ) ::Tools::Serilizer::makePair< ::Tools::Serilizer::Serilizable >( EXTRACT_NAME( _value ), _value )



// This does work and should be simplified by....
for( BOOST_AUTO( i, m_pages.begin( ) ); i != m_pages.end( ); ++i )
{
    obj << SERILIZE_CLASS( *i->second );
}

// this code but itdoes not compile
std::for_each( m_pages.begin( ), m_pages.end( ), 
obj << SERILIZE_CLASS( boost::lambda::bind( &Pages::value_type::second, boost::lambda::_1 ) ) );

Finally this is the resulting error code:

error C2664: 'Tools::Serilizer::makePair' : cannot convert parameter 2 from 'const boost::lambda::lambda_functor' to 'const Tools::Serilizer::Serilizable &'

Any hints how to solve this ?

回答1:

I think your problem is mixing lambdas (that is functions) with the values returned by the lambdas:

For example:

boost::lambda::bind( &Pages::value_type::second, boost::lambda::_1 )

Returns a function.

So calling serialize_class(...) with its result doesn't make sense to me.

That said, I haven't studied your code in much depth. I found it a bit confusing.