Boost :: ptr_vector exemption, not related documentation

I am using boost 1.37 and I am trying to use boost :: ptr_vector and transfer ownership of it so that I can return it from a function. Looking at the documentation on speeding up ( http://www.boost.org/doc/libs/1_36_0/libs/ptr_container/doc/tutorial.html#new-functions )

std::auto_ptr< boost::ptr_deque<animal> > get_zoo() { boost::ptr_deque<animal> result; ... return result.release(); // give up ownership } ... boost::ptr_deque<animal> animals = get_zoo(); 

I tried:

 #include "iostream" #include <boost/ptr_container/ptr_vector.hpp> class Item { public: int my_val; Item() : my_val(0) { } }; class MyClass { private: boost::ptr_vector<Item> items_; public: MyClass() { for (int i = 0; i < 10; ++i) items_.push_back(new Item); } std::auto_ptr<boost::ptr_vector<Item> > getData() { return items_.release(); } }; int totalItems(boost::ptr_vector<Item> items) { int total = 0; boost::ptr_vector<Item>::iterator it; for (it = items.begin(); it != items.end(); ++it) total += (*it).my_val; return total; } int main(int argc, char **argv) { MyClass cls; boost::ptr_vector<Item> items = cls.getData(); int total = totalItems(items); fprintf(stdout, "I found %d items!\n", total); return 0; 

}

Compiler Errors:

 In function 'int main(int, char**)': error: conversion from 'std::auto_ptr<boost::ptr_vector<Item, boost::heap_clone_allocator, std::allocator<void*> > >' to non-scalar type 'boost::ptr_vector<Item, boost::heap_clone_allocator, std::allocator<void*> >' requested 

Is this a bug in the acceleration documentation? Can I get auto_ptr and dereference to return ptr_vector?

+4
source share
1 answer

Yes, this is a documentation error. As with auto_ptr (and any other type with semantics of transferring ownership), the transfer constructor to ptr_deque is explicit , so you cannot use the initialization form = . You must do:

 boost::ptr_vector<Item> items(cls.getData()); 

In addition, this is only a textbook that is incorrect; the actual class reference shows this correctly. If you look here , you will see an announcement:

  explicit reversible_ptr_container( std::auto_ptr<reversible_ptr_container> r ); 

Note explicit .

+6
source

Source: https://habr.com/ru/post/1301520/


All Articles