<div class="gmail_quote">On Sun, Nov 7, 2010 at 1:20 AM, Peter Wright <span dir="ltr">&lt;<a href="mailto:pete@flooble.net">pete@flooble.net</a>&gt;</span> wrote:<br><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

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

<br>
Another way to fix it (and also avoid the reference-storing problem)<br>
requires using Boost::Bind - change SortByName to this:<br>
<br>
&lt;snip/&gt;<br>
...and the A.sort call to this:<br>
<br>
    A.sort( boost::bind(SortByName&lt;Graph, edgeDescriptor&gt;(), g, _1, _2) );<br></blockquote><div><br>It does avoid the reference-storing problem. I believe it does so by copying the entire graph. boost::bind() stores its parameters by value.<br>
<br>If that is in fact excessive, you could write:<br><br>A.sort( boost::bind(SortByName&lt;Graph, edgeDescriptor&gt;(), boost::ref(g), _1, _2) );<br><br>Now you&#39;re passing a reference again.<br><br>It&#39;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: &quot;Hmm, sort(), I see no reason why any of its parameters should persist beyond its return.&quot; I assume that the whole boost::bind() value will be destroyed on return from sort(), therefore the bound reference to g as well.<br>
</div></div>

