使用boost lambda构造创建初始化智能ptrs的容器

时间:2014-10-09 12:30:14

标签: c++ boost lambda

我正在尝试使用boost lambda库而且我已经卡住了。 我试图创建并初始化一个共享指针容器,并且不能克服将右值传递给非const引用。

代码段:

#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,但我无法编译它。

我应该如何修改代码以获得创建和初始化智能指针容器所需的效果?

1 个答案:

答案 0 :(得分:3)

您正在混合使用boost::bindboost::lambda::bind不完全兼容的内容:

试试这个:

#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