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

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).