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

Creating a container of initialized smart ptrs with boost lambda construct

本文关键字:智能 ptr 初始化 创建 boost lambda 使用      更新时间:2023-10-16

我正在试用boost lambda库,但遇到了问题。我正在尝试创建和初始化一个共享指针的容器,无法将右值传递给非常量引用。

代码片段:

#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的常量,但无法编译。

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

您正在混合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