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();
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?
source share