Инициализация контейнеров полиморфных указателей - PullRequest
4 голосов
/ 09 мая 2011

Могу ли я инициализировать полиморфный boost::ptr_vector с boost::assign::list_of?

#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/assign/list_of.hpp>

boost::ptr_vector<Animal> ls = boost::assign::list_of(new Ant)(new Bee)(new Cat);

Не удается скомпилировать:

error: no match for call to '(boost::assign_detail::generic_list<Ant*>) (Bear*)'

Замена boost::ptr_vector<Animal> на std::vector<Animal*> дает то же самоеошибка.


Два человека предложили вручную указать аргумент шаблона от Animal* до list_of:

boost::assign::list_of<Animal*>(new Ant)(new Bear)(new Cat)

Но он все равно не работает:

boost/ptr_container/ptr_sequence_adapter.hpp: In static member function 'static boost::ptr_container_detail::sequence_config<T, VoidPtrSeq>::U* boost::ptr_container_detail::sequence_config<T, VoidPtrSeq>::get_const_pointer(Iter) [with Iter = std::_Deque_iterator<Animal*, Animal*&, Animal**>, T = Animal, VoidPtrSeq = std::vector<void*, std::allocator<void*> >, boost::ptr_container_detail::sequence_config<T, VoidPtrSeq>::U = Animal]':
boost/ptr_container/detail/reversible_ptr_container.hpp:95:71:   instantiated from 'static boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::Ty_* boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::null_clone_allocator<allow_null_values>::allocate_clone_from_iterator(Iter) [with Iter = std::_Deque_iterator<Animal*, Animal*&, Animal**>, bool allow_null_values = false, Config = boost::ptr_container_detail::sequence_config<Animal, std::vector<void*, std::allocator<void*> > >, CloneAllocator = boost::heap_clone_allocator, boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::Ty_ = Animal]'
boost/ptr_container/detail/scoped_deleter.hpp:70:21:   instantiated from 'boost::ptr_container_detail::scoped_deleter<T, CloneAllocator>::scoped_deleter(InputIterator, InputIterator) [with InputIterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>, T = Animal, CloneAllocator = boost::ptr_container_detail::reversible_ptr_container<boost::ptr_container_detail::sequence_config<Animal, std::vector<void*, std::allocator<void*> > >, boost::heap_clone_allocator>::null_clone_allocator<false>]'
boost/ptr_container/detail/reversible_ptr_container.hpp:212:44:   instantiated from 'void boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::clone_back_insert(ForwardIterator, ForwardIterator) [with ForwardIterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>, Config = boost::ptr_container_detail::sequence_config<Animal, std::vector<void*, std::allocator<void*> > >, CloneAllocator = boost::heap_clone_allocator]'
boost/ptr_container/detail/reversible_ptr_container.hpp:303:13:   instantiated from 'void boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::constructor_impl(I, I, std::forward_iterator_tag) [with I = std::_Deque_iterator<Animal*, Animal*&, Animal**>, Config = boost::ptr_container_detail::sequence_config<Animal, std::vector<void*, std::allocator<void*> > >, CloneAllocator = boost::heap_clone_allocator]'
boost/ptr_container/detail/reversible_ptr_container.hpp:378:13:   instantiated from 'boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::reversible_ptr_container(InputIterator, InputIterator, boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::allocator_type&) [with InputIterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>, Config = boost::ptr_container_detail::sequence_config<Animal, std::vector<void*, std::allocator<void*> > >, CloneAllocator = boost::heap_clone_allocator, boost::ptr_container_detail::reversible_ptr_container<Config, CloneAllocator>::allocator_type = std::allocator<void*>]'
boost/ptr_container/ptr_sequence_adapter.hpp:178:36:   instantiated from 'boost::ptr_sequence_adapter<T, VoidPtrSeq, CloneAllocator>::ptr_sequence_adapter(InputIterator, InputIterator) [with InputIterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>, T = Animal, VoidPtrSeq = std::vector<void*, std::allocator<void*> >, CloneAllocator = boost::heap_clone_allocator]'
boost/ptr_container/ptr_vector.hpp:45:9:   instantiated from 'boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(InputIterator, InputIterator) [with InputIterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>, T = Animal, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]'
boost/assign/list_of.hpp:163:46:   instantiated from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert(const Container*, boost::assign_detail::default_type_tag) const [with Container = boost::ptr_vector<Animal>, DerivedTAssign = boost::assign_detail::generic_list<Animal*>, Iterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>]'
boost/assign/list_of.hpp:142:54:   instantiated from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert_to_container() const [with Container = boost::ptr_vector<Animal>, DerivedTAssign = boost::assign_detail::generic_list<Animal*>, Iterator = std::_Deque_iterator<Animal*, Animal*&, Animal**>]'
boost/assign/list_of.hpp:436:81:   instantiated from 'boost::assign_detail::generic_list<T>::operator Container() const [with Container = boost::ptr_vector<Animal>, T = Animal*]'

Ответы [ 4 ]

5 голосов
/ 09 мая 2011

Я сделаю удар.Я думаю list_of - это шаблон, который выводит аргумент шаблона.Поскольку ваш тип аргумента является производным классом, он считает, что это тип контейнера.Родственные классы не совпадают.

Возможно, вам придется явно указать аргумент шаблона, например:

boost::ptr_vector<Animal> ls = boost::assign::list_of<Animal*>(new Ant)(new Bee)(new Cat);
3 голосов
/ 10 мая 2011

Вы не должны использовать list_of для назначения в Boost Pointer Collections. Если один из конструкторов выбрасывает, уже созданные экземпляры еще не были добавлены в контейнер, и они не будут удалены list_of (что ожидает значения, а не полиморфные типы).

Boost.Assignment предоставляет вместо ptr_list_of(). Однако он может добавлять только однородные элементы (элементы одного типа).

Итак, я думаю, что Boost.Assignment не очень полезен для разнородных коллекций.

1 голос
/ 10 мая 2011

Если разрешена двухэтапная инициализация, boost::assign::push_back может соответствовать Цель:

ptr_vector<Animal> ls;
assign::push_back( ls )(new Ant)(new Bee)(new Cat);

К сожалению, я не знаю краткого способа инициализации за один шаг. Если мы подготовим какой-то конвертер, как показано ниже, вероятно, один шаг инициализация может быть сделана.

template< class T >
struct converter : std::vector< T > {
  template< class Container >
  operator Container() const {
    Container c;
    for ( const_iterator i = begin(), e = end();  i != e;  ++ i )
      c.push_back( *i );
    return c;
  }
};

template< class T >
converter< typename T::value_type > convert( T const& x ) {
  converter< typename T::value_type > c;
  c.assign( x.begin(), x.end() );
  return c;
}

ptr_vector<Animal> ls =
  convert( assign::list_of<Animal*>(new Ant)(new Bee)(new Cat) );

Это может быть долго, хотя ...

0 голосов
/ 09 мая 2011

Эта программа компилируется, но я не гарантирую ее семантику:

#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/assign/list_of.hpp>

struct Animal {};

struct Ant : Animal {};
struct Bee : Animal {};
struct Cat : Animal {};

boost::ptr_vector<Animal> ls=boost::assign::list_of<Animal>(Ant())(Bee())(Cat());

int main() {}

<ч /> РЕДАКТИРОВАТЬ: Это также компилирует:

std::vector<Animal*> ls=boost::assign::list_of<Animal*>(new Ant())(new Bee())(new Cat());
...