Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2005-11-22 18:22:30


Axter <boost <at> axter.com> writes:

>
> http://www.boost.org/libs/ptr_container/doc/ptr_container.html
>
> I'm currently in the middle of writing an article making the comparison
between the above boost pointer
> containers and the following clone smart pointers:
> http://code.axter.com/copy_ptr.h
> http://code.axter.com/cow_ptr.h
>
> I'm performing my test using boost_1_33_0, and VC++ 7.1 compiler.
> I'm comparring vector<copy_ptr<T> > to boost::ptr_vector<T>
>
> I want to make sure that my article is accurate and complete, so I'm posting
this question.
>
> When trying to compile using copy constructor for ptr_vector, I get a compile
error.
> boost::ptr_vector<Shape>
CopyViaContainerCopyConstructor(ContainerOfOriginalShapes); //Will
> not compileContainerType
>
> boost::ptr_vector<Shape> CopyViaContainerCopyConstructor =
ContainerOfOriginalShapes; //Will
> not compile
>
> Is this by design, and if so, why doesn't the boost pointer container have a
copy constructor like the STL
> containers have?

Please read the FAQ and the conventions sections.

> Another standard STL container interface that fails to compile using
ptr_container is push_back via
> dereferenced iterator.
> Example:
> for(it = ContainerOfOriginalShapes.begin();it !=
ContainerOfOriginalShapes.end();++it)
>
> CopyViaContainerForLoopIterator.push_back(*it);
>
> Is this also by designed, and if so, why?

To make it more obvious that the container takes ownership of heap allocated
objects:

cont.push_back( new_clone(*it) );

The idea if letting ptr_cont<T> be replacable with cont<T> turned out
not to be a very important goal, and hence simplicity in the interface was
better.
 
> I also notice that the follow code fails at runtime.
> boost::ptr_vector<Shape>
CopyViaContainerForLoopOp(ContainerOfOriginalShapes.size());
>
> for(i = 0;i < ContainerOfOriginalShapes.size();++i)
>
> CopyViaContainerForLoopOp[i] = ContainerOfOriginalShapes[i];
>
> However, if I do the following, then it works:
>
> //Try creating the right size for the container via push_back
>
> for(i = 0;i < ContainerOfOriginalShapes.size();++i)
>
> CopyViaContainerForLoopOp.push_back(Shape::AbstractFactory(SourceData[i][0],
SourceData[i][1]));
>
> //Again test copy via for loop using operator[]
>
> for(i = 0;i < ContainerOfOriginalShapes.size();++i)
>
> CopyViaContainerForLoopOp[i] = ContainerOfOriginalShapes[i];
>
> Is there a problem with the way ptr_vector gets constructed when size argument
is used for constructor with
> out a default value?

there is no way to specify a default agument here. Please read the documentation
for the function.
 
> When using the VC++ 7.1 compiler, I notice I get a compile warning "resolved
overload was found by
> argument-dependent lookup"

This is a stupid warning.

>
> I also welcome any additional input that may be useful for the article.

I'd like to see it when you're done.

-Thorsten


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk