On Sun, Nov 7, 2010 at 1:20 AM, Peter Wright <pete@flooble.net> wrote:

It is dicey in one way, in that it stores a *reference* to its Graph -
so if the SortByName object outlives the Graph then it could blow up
spectacularly. But the alternative of copying the entire graph seemed
a bit excessive.

Another way to fix it (and also avoid the reference-storing problem)
requires using Boost::Bind - change SortByName to this:

<snip/>
...and the A.sort call to this:

   A.sort( boost::bind(SortByName<Graph, edgeDescriptor>(), g, _1, _2) );

It does avoid the reference-storing problem. I believe it does so by copying the entire graph. boost::bind() stores its parameters by value.

If that is in fact excessive, you could write:

A.sort( boost::bind(SortByName<Graph, edgeDescriptor>(), boost::ref(g), _1, _2) );

Now you're passing a reference again.

It's unfortunate that you have to know whether a given function stores its parameters to know whether passing a reference might incur lifespan troubles. My impulse would be to think: "Hmm, sort(), I see no reason why any of its parameters should persist beyond its return." I assume that the whole boost::bind() value will be destroyed on return from sort(), therefore the bound reference to g as well.