Boost logo

Boost Users :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2002-10-03 09:03:36


Hi Bjorn,

On Wed, 2 Oct 2002, [iso-8859-1] Björn Lindberg wrote:
[snip]
yg-boo> 1) Why is a lot of the function arguments made by value rather than
yg-boo> reference? As an example, consider the BFS algorithm, where the
yg-boo> documentation states that
[snip]

One of the usage scenarios that we wanted to support with the algorithms
was creating visitor objects on the fly, within the argument list of the
call to the graph algorithm. In this situation, the visitor object is a
temporary object. Now there is a truly unfortunate rule in the C++ std
that says a temporary cannot be bound to a non-const reference parameter.
So we had to decide whether we wanted to support this kind of usage and go
with call-by-value, or not and go with call-by-reference. We chose
call-by-value, following in the footsteps of the STL (which passes
functors by value).

yg-boo> It would be much easier to use the visitor objects for keeping state
yg-boo> throughout the algorithm if it was passed by reference instead.
yg-boo>
yg-boo> 2) Non-constness. It seems impossible to use BGL together with
yg-boo> code that is "const-correct". If, for instance, I have a function
yg-boo> implementing some kind of examining algorithm on a graph (so the
yg-boo> graph is passed to it as const), and this algorithm uses
yg-boo> breadth_first_search with a visitor that doesn't modify the graph,
yg-boo> it still can't be const when passed to breadth_first_search. This
yg-boo> only leaves the options of either (i) casting away constness in
yg-boo> the call, which is bad because it could introduce bugs later on,
yg-boo> or (ii) leaving out const altogether which is not good either.
yg-boo>
yg-boo> Maybe I've just misunderstood something and this is possible, or
yg-boo> perhaps it is impossible to implement BGL in this way?

If you look at the docs for BFS, you'll see that one version takes the
graph non-const, and the other takes the graph as a const reference. The
reason is that BFS modifies the color map during the search. The version
of the algorithm that takes the graph non-const may be getting the color
map from the graph, as an internal property map, so in this case BFS is
modifying the graph in some sense. The version of BFS that takes the graph
as a const reference has a separate parameter for the color map.

I suggest that you use the non-named template parameter version that takes
the graph by const reference.

Regards,
Jeremy

----------------------------------------------------------------------
 Jeremy Siek http://php.indiana.edu/~jsiek/
 Ph.D. Student, Indiana Univ. B'ton email: jsiek_at_[hidden]
 C++ Booster (http://www.boost.org) office phone: (812) 855-3608
----------------------------------------------------------------------


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