什么是boost :: ptr_vector :: pop_front()返回类型?

时间:2012-03-12 10:51:36

标签: c++ boost smart-pointers

我使用auto_type boost::ptr_vector::pop_front()

时遇到了一些问题

返回类型auto_type的定义如下

typedef ptr_container_detail::static_move_ptr<Ty_,Deleter> auto_type;

然而,分配给std :: auto_ptr,boost :: shared_ptr,失败......

Error   1   error C2440: 'initializing' : cannot convert from 'boost::ptr_container_detail::static_move_ptr<T,Deleter>' to 'std::auto_ptr<_Ty>' 
Error   1   error C2440: 'initializing' : cannot convert from 'boost::ptr_container_detail::static_move_ptr<T,Deleter>' to 'std::tr1::shared_ptr<_Ty>'  
Error   1   error C2440: 'initializing' : cannot convert from 'boost::ptr_container_detail::static_move_ptr<T,Deleter>' to 'std::tr1::weak_ptr<_Ty>'

通过查看实现,看起来pop_front应该返回一个指向前面的智能指针并返回该值。

auto_type pop_front()  
{  
    BOOST_ASSERT( !this->empty() &&  
                  "'pop_front()' on empty container" );   
    auto_type ptr( static_cast<value_type>( this->base().front() ) );   
                                 // nothrow   
    this->base().pop_front();    // nothrow  
    return ptr_container_detail::move( ptr );   
} 

但是...... auto_type是什么类型的?

1 个答案:

答案 0 :(得分:1)

您可以将auto_type视为std :: auto_ptr的不可复制形式。请注意,释放对象时,指针将从容器中移除,容器大小会缩小。对于存储空值的容器,我们可以利用auto_type可转换为bool:

if( ptr_vector< nullable<T> >::auto_type r = vec.pop_back() )
{
  ...
}