使用boost lambda构造创建初始化的智能ptr的容器
Creating a container of initialized smart ptrs with boost lambda construct
我正在试用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::bind
和boost::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
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 优先顺序:智能指针和类析构函数
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- CLANG 编译器 说:变量"PTR"可能未初始化
- 智能指针作为无序映射键,并通过引用进行比较
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 智能指针概念所有权和寿命
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 尝试使用智能指针时引发异常
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 我可以制作指向智能指针的智能指针吗?
- 通过智能指针和转换对基本模板参数进行模板推导
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 带智能ptr的Pimpl-为什么需要构造函数/析构函数
- 从没有所有权的容器中清理智能 Ptr 的模式
- 将现有值分配给智能ptr
- 使用boost lambda构造创建初始化的智能ptr的容器
- 如何转发声明boost智能ptr
- 正确使用std智能指针以确保ptr安全