[Boost-users] Delete and free all elements of ptr_vector

90 views
Skip to first unread message

Florian Lindner via Boost-users

unread,
Sep 7, 2017, 5:10:08 AM9/7/17
to boost...@lists.boost.org, Florian Lindner
Hello,

in my project we have a simple ptr_vector that has a member function:

void deleteElements ()
{
for ( CONTENT_T * elem : _content ) {
assertion ( elem != NULL );
delete ( elem );
}
}

I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
stored in the container. How can I do that?

Thanks,
Florian
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users

Thorsten Ottosen via Boost-users

unread,
Sep 7, 2017, 5:28:23 AM9/7/17
to boost...@lists.boost.org, Thorsten Ottosen
Den 07-09-2017 kl. 11:09 skrev Florian Lindner via Boost-users:
> Hello,
>
> in my project we have a simple ptr_vector that has a member function:
>
> void deleteElements ()
> {
> for ( CONTENT_T * elem : _content ) {
> assertion ( elem != NULL );
> delete ( elem );
> }
> }
>
> I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
> stored in the container. How can I do that?

boost::ptr_vector<Foo> container;
...
container.clear()

kind regards

-Thorsten

Richard Hodges via Boost-users

unread,
Sep 7, 2017, 5:45:18 AM9/7/17
to boost...@lists.boost.org, Richard Hodges
Complete example:

#include <iostream>
#include <boost/utility.hpp>
#include <boost/ptr_container/ptr_vector.hpp>

struct Foo : boost::noncopyable
{
Foo() { std::cout << "made a Foo\n"; }
~Foo() { std::cout << "destroyed a Foo\n"; }
};

int main() {

boost::ptr_vector<Foo> foos;

foos.push_back(new Foo());
foos.push_back(new Foo());

foos.clear();

}

Florian Lindner via Boost-users

unread,
Sep 7, 2017, 6:58:03 AM9/7/17
to Thorsten Ottosen, boost...@lists.boost.org, Florian Lindner
Thanks for your answer!

One more question: Is there a way to store an incomplete (forward declared) type. I get the compiler error message:

/home/florian/precice/src/mesh/Group.cpp:70:45: required from here
/usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:111:17: error: invalid use of incomplete type
'class precice::mesh::Vertex'
BOOST_ASSERT( typeid(*res) == typeid(*x) &&
In file included from /home/florian/precice/src/mesh/Group.cpp:1:0:
/home/florian/precice/src/mesh/Group.hpp:8:11: note: forward declaration of 'class precice::mesh::Vertex'
class Vertex;
^~~~~~
In file included from /usr/include/boost/assert.hpp:58:0,
from /usr/include/boost/range/size.hpp:23,
from /usr/include/boost/range/functions.hpp:20,
from /usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:29,
from /usr/include/boost/ptr_container/ptr_sequence_adapter.hpp:20,
from /usr/include/boost/ptr_container/ptr_vector.hpp:20,
from /home/florian/precice/src/mesh/Group.hpp:4,
from /home/florian/precice/src/mesh/Group.cpp:1:
/usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:111:17: error: invalid use of incomplete type
'class precice::mesh::Vertex'
BOOST_ASSERT( typeid(*res) == typeid(*x) &&

So I assume no, or is there?

Thanks,
Florian

Thorsten Ottosen via Boost-users

unread,
Sep 7, 2017, 7:27:00 AM9/7/17
to boost...@lists.boost.org, Thorsten Ottosen
Den 07-09-2017 kl. 12:57 skrev Florian Lindner via Boost-users:
> Thanks for your answer!
>
> One more question: Is there a way to store an incomplete (forward declared) type. I get the compiler error message:
>

Please see the FAQ:

http://www.boost.org/doc/libs/1_64_0/libs/ptr_container/doc/faq.html

Short answer: yes, but not at instantiation time.

Thorsten Ottosen via Boost-users

unread,
Sep 7, 2017, 9:55:06 AM9/7/17
to boost...@lists.boost.org, Thorsten Ottosen
Den 07-09-2017 kl. 11:09 skrev Florian Lindner via Boost-users:
> Hello,
>
> in my project we have a simple ptr_vector that has a member function:
>
> void deleteElements ()
> {
> for ( CONTENT_T * elem : _content ) {
> assertion ( elem != NULL );
> delete ( elem );
> }
> }
>
> I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
> stored in the container. How can I do that?

Note also that the destructor automatically cleans up.

-T
Reply all
Reply to author
Forward
0 new messages