2014-10-09 33 views
0

我正在嘗試boost lambda庫,並且卡住了。 我想創建並初始化一個共享指針的容器,並且無法克服將右值傳遞給非const引用。使用boost lambda構造器創建初始化智能ptrs的容器

代碼片段:

#include <iostream> 
#include <boost/lambda/lambda.hpp> 
#include <boost/lambda/construct.hpp> 
#include <algorithm> 
#include <boost/iterator/counting_iterator.hpp> 
#include <boost/bind.hpp> 
#include <boost/smart_ptr.hpp> 

class Base 
{ 
public: 
    explicit Base (int i) : i_(i) { std::cout << "Creating: " << i << std::endl; } 
    ~Base(){ std::cout << "Deleting: " << i_ << std::endl; } 
private: 
    int i_; 
}; 

int main() 
{ 
    using namespace boost; 

    std::vector< boost::shared_ptr<Base> > bsp; 
    bsp.reserve(10); 

    std::transform(boost::counting_iterator<int>(0), boost::counting_iterator<int>(10), std::back_inserter(bsp), 
      bind< boost::shared_ptr<Base> >(lambda::constructor< boost::shared_ptr<Base> >(), bind<Base*>(lambda::new_ptr<Base>(), lambda::_1))); 

    return 0; 
} 

我不斷收到以下編譯錯誤:

/usr/include/boost/bind/bind.hpp:243:16: error: no matching function for call to object of type 'boost::lambda::constructor<boost::shared_ptr<Base> >' 
     return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]); 
       ^~~~~~~~~~~~~~~~~~~~~~~~~~ 
/usr/include/boost/bind/bind_template.hpp:47:27: note: in instantiation of function template specialization 'boost::_bi::list1<boost::_bi::bind_t<Base *const, 
     boost::lambda::new_ptr<Base>, boost::_bi::list1<boost::arg<1> > > >::operator()<boost::shared_ptr<Base>, boost::lambda::constructor<boost::shared_ptr<Base> >, 
     boost::_bi::list1<const int &> >' requested here 
     BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0); 
         ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/stl_algo.h:4951:14: note: in instantiation of function template specialization 
     'boost::_bi::bind_t<boost::shared_ptr<Base>, boost::lambda::constructor<boost::shared_ptr<Base> >, boost::_bi::list1<boost::_bi::bind_t<Base *const, boost::lambda::new_ptr<Base>, 
     boost::_bi::list1<boost::arg<1> > > > >::operator()<int>' requested here 
     *__result = __unary_op(*__first); 
        ^
t58_complex_lamdba_1.cpp:33:10: note: in instantiation of function template specialization 'std::transform<boost::counting_iterator<int, boost::use_default, boost::use_default>, 
     std::back_insert_iterator<std::vector<boost::shared_ptr<Base>, std::allocator<boost::shared_ptr<Base> > > >, boost::_bi::bind_t<boost::shared_ptr<Base>, 
     boost::lambda::constructor<boost::shared_ptr<Base> >, boost::_bi::list1<boost::_bi::bind_t<Base *const, boost::lambda::new_ptr<Base>, boost::_bi::list1<boost::arg<1> > > > > >' 
     requested here 
    std::transform(boost::counting_iterator<int>(0), boost::counting_iterator<int>(10), std::back_inserter(bsp), 
     ^
/usr/include/boost/lambda/construct.hpp:36:5: note: candidate function [with A1 = Base *] not viable: no known conversion from 'Base *' to 'Base *&' for 1st argument 
    T operator()(A1& a1) const { 
    ^

我試圖操縱Base*的常量性和bind返回類型,和lambda :: make_const,但我無法編譯。

我該如何修改代碼以獲得創建和初始化智能指針容器的所需效果?

回答

3

你混合boost::bindboost::lambda::bind這不完全compatibile:

試試這個:

#include <boost/lambda/bind.hpp> 
// ... 
std::transform(boost::counting_iterator<int>(0), boost::counting_iterator<int>(10), std::back_inserter(bsp), 
     lambda::bind< boost::shared_ptr<Base> >(lambda::constructor< boost::shared_ptr<Base> >(), lambda::bind<Base*>(lambda::new_ptr<Base>(), lambda::_1))); 

DEMO

+0

該訣竅。我想知道爲什麼lambda中有一個綁定,知道我已經學會了這個難題。謝謝! – 2014-10-09 12:39:27

+0

@SebastianKramer文檔解釋了[它們與衆不同](http://www.boost.org/doc/libs/1_56_0/doc/html/lambda/s08.html#idp325131008) – 2014-10-09 12:49:05

+0

嘿,謝謝。我還沒有到那個部分,但我一定會看看它。 – 2014-10-09 12:50:35