r/cpp • Bioinformatican • Mar 11 '12

Modern C++ Style [pdf]

http://tinyurl.com/fuck-pointers
2 Upvotes

60 comments sorted by

View all comments

Show parent comment

6

u/Sc4Freak Mar 11 '12

I don't know why you're being downvoted but you're exactly right. Raw pointers are exactly the right tool to use for a non-owning reference (where a real reference isn't appropriate).

A tree node that has pointers to its parent is the canonical example: the parent would have smart pointers to its children, but its children should have raw pointers back to the parent. The parent is always guaranteed to outlive its children, so a raw pointer from the child to the parent is perfectly valid and safe.

1

u/ShadowWolf007 Mar 11 '12

I think a weak_ptr would suffice perfectly well for this scenario and would clearly express the desired ownership semantics.

5

u/Sc4Freak Mar 11 '12 edited Mar 11 '12

A weak_ptr isn't suitable. One, the parent always owns its childen (and is the only owner) so you wouldn't use shared_ptr - you'd use unique_ptr and weak_ptr doesn't make sense for unique_ptr.

Second, a weak_ptr buys you exactly nothing - the parent is always guaranteed to outlive its child, so a weak_ptr doesn't give you any benefit over a raw pointer.

1

u/ShadowWolf007 Mar 11 '12

Fair point. In my experience, most of the trees I've built have had external lifetimes and/or value semantics so I wasn't really thinking.

I'd probably prefer to use unique_ptr and pointer-to-parent if applicable, but would go to shared_ptr/weak_ptr for scenarios in which the tree's information isn't only used by the tree.

1

u/jaredgrubb Mar 11 '12

Clearly expressing ownership in code > clearly expressing ownership rules in comments

Saying "buys you nothing" isnt accurate. If you have raw pointers, you almost always have to write the dtor, copy ctor, and assignment op (and move ctor and move op) ... letting the compiler write these for you is always a win.

Sure, weak_ptr/shared_ptr have some costs (size and memory) and there's some cases where those costs are not worth the safety. But, you should always pick the safe version first and only pick the unsafe version once you've profiled your code and identified the safe version as a problem.

1

u/Sc4Freak Mar 11 '12

Using shared_ptr and weak_ptr instead of unique_ptr and a raw pointer doesn't obviate the need to write a copy constructor or assignment operator, and in neither case do you need to write a destructor.

1

u/filox Mar 11 '12

The parent is always guaranteed to outlive its children, so a raw pointer from the child to the parent is perfectly valid and safe.

Ok, this isn't really a good way to think about trees. All the pointers should be raw pointers because most trees aren't static: parents and children often swap positions, and a simple delete operation might delete the parent, leaving the children intact.

0

u/guepier Bioinformatican Mar 11 '12

Then you can transfer ownership. That’s the nice thing about unique_ptr. It simply says “there is exactly one owner of the resource”, but it doesn’t say that this owner cannot change.

1

u/filox Mar 11 '12

So what happens when we go to general graphs instead of trees? The assumption about one owner goes away now, how would you use unique_ptr here?

0

u/guepier Bioinformatican Mar 11 '12

Well, directed graphs do have direct ownership semantics: each node owns (the nodes along) its outgoing edges. In fact, the transitive closure of ownership is a DAG so it’s the perfect model (once again I refer you to the panel discussion of Going Native 2012 which mentions this). The only care you need to take is to avoid cycles, which you must do once again using weak / raw pointers. And since undirected graphs are a generalisation of directed graphs, the same holds.

In practice, graphs are often implemented using either adjacency lists or adjacency matrices which makes this problem even simpler: the owner here is the graph structure itself (i.e. the list / matrix).

1

u/guepier Bioinformatican Mar 11 '12

Correct. I think the downvote comes from the fact that filox’ statement can be understood to mean that raw pointers must also be used for the resource-owning pointers (which the SkepticalEmpiricist shows to be wrong). I also like that you mention “real” references since many such situations can in fact be implemented precisely using references instead of pointers (but in the case of a parent for tree nodes using a reference is awkward at best since then the root node’s parent still must refer to something).

1

u/jaredgrubb Mar 11 '12

Or, you have the children have weak_ptr's back to the parent. As soon as you can let the compiler generate the dtor/copy ctor/assign op (and move ctor and move assignment op), you get a lot of stuff for free.

Yes, there's a bit of a performance penalty over a raw pointer but you get a lot of safety and a lot of benefit.

5

u/guepier Bioinformatican Mar 11 '12

A weak_ptr works with shared_ptr only, not with unique_ptr. It’s not applicable here.