|
Boost Users : |
Subject: Re: [Boost-users] [BGL] Unable to sort list of edge_descriptors
From: Nat Linden (nat_at_[hidden])
Date: 2010-11-08 08:57:01
On Sun, Nov 7, 2010 at 1:20 AM, Peter Wright <pete_at_[hidden]> 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.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net