2012-04-15 127 views
0

我想通過使用boost :: lambda簡化我的代碼。這裏是我的代碼:boost :: lambda std :: map

// 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))); 

最後,這是所產生的錯誤代碼:

錯誤C2664: '工具:: Serilizer :: makePair':不能將參數2從「常量的boost ::拉姆達:: lambda_functor'到'const Tools :: Serilizer :: Serilizable &'

任何提示如何解決這個問題?

+0

Boost.Lambda已正式被棄用;請使用[Boost.Phoenix](http://www.boost.org/libs/phoenix/)代替新代碼。 – ildjarn 2012-04-15 16:47:27

回答

0

我覺得你的問題是混合的lambda(即函數)由lambda表達式返回的值:

例如:

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

返回功能。

所以調用serialize_class(...)其結果對我來說沒有意義。

這就是說,我沒有深入研究過你的代碼。我發現它有點混亂。

+0

我認爲你是對的。使用boost :: lambda :: bind(&Pages :: value_type :: second,boost :: lambda :: _ 1)我只想訪問map map :: pair的第二項。但結果是一個仿函數。任何建議如何處理? – Mark 2012-04-15 17:19:26

+0

對不起,我不知道。 – cdiggins 2012-04-15 17:40:04