On 17.07.2015 15:00, Paul wrote:
> Some have said on other threads to use raw pointers instead of
> unique_ptr when iterating in a container.
" iterating in a container."
I don't understand.
It looks like all 'gurus' from c++ say to use unique pointers.
It is all about keeping RAII in dynamic alocated objects. About
who own what. If you container of pointers is a main owner,
of objects, no other long living object 'borrow it',
unique_ptr is a good idea.
There:
https://www.youtube.com/user/CppCon
somewhere is quite good lecture about poiters and optimalizations,
and speaker said if you have function that not relocate ownership,
raw pointers are best solution. Chain of function have raw pointer
as argument, and the top function is call with unique_ptr
as an argument.
There is something about this topic
https://www.youtube.com/watch?v=xnqTKD8uD64
https://github.com/CppCon/CppCon2014/blob/master/Presentations/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style%20-%20Herb%20Sutter%20-%20CppCon%202014.pdf
And lecture is very interesting, but I'm sure there was one more focused
on pointers. Sorry, I don't remember which one.
> Suppose, for example, that I'm designing a tree where the number of
> nodes descending from any particular node is variable. Any reason
> not to put nodes in vectors of std::unique_ptr<T>
If you don't keep pointers (raw in case of uniqueptr) in other places
I think it is nice idea.
In a node you erase one node (pointing at children) from vector. This
is unique_ptr, so it runs destructor of targeted node. And node have a
vector of unique ptr, so destructor of vestor and all pointers is
called...
You erased one node, all children (recursively) are erased and its
memory freed automatically.
One function less to write ;-)
But it works only because it is a tree.
bartekltg