views:

101

answers:

1

I'm using boost 1.37, and I'm trying to use a boost::ptr_vector, and transfer ownership of it so I can return it from a function. Looking at the boost documentation (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 glitch in the boost documentation? Can I get an auto_ptr and dereference to get the ptr_vector back?

+5  A: 

Yes, it is a documentation bug. As with auto_ptr (and any other type with ownership transfer semantics), the transferring constructor on ptr_deque is explicit, so you cannot use = initializer form. You have to do:

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

Also, it's only the tutorial that is wrong; the actual class reference properly shows this. If you look here, you'll see the declaration:

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

Note explicit.

Pavel Minaev
Thanks for the heads up, everything works now.
kevbo