Boost logo

Boost :

Subject: Re: [boost] Boost Graph Library: why are `source()`/`target()` non-member functions?
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2013-04-20 16:18:02


On Fri, 19 Apr 2013, Max Moroz wrote:

> Why is `source()` not a member function of the graph class?
>
> (See http://www.boost.org/doc/libs/1_53_0/libs/graph/doc/index.html for
> description, and boost/graph/adjacency_list.hpp and
> boost/graph/adjacency_matrix.hpp for implementation.)
>
> I thought the reason was that it was a generic algorithm. But it's not.
> There's no interface (beyond `source()` itself) exposed by an edge
> descriptor that would allow `source()` to avoid digging into the
> implementation.
>
> If I interpret the code correctly, `source()` reads directly from the
> internal data structure of the edge descriptor
> (`detail::edge_base.m_source`), which I am pretty sure is the
> implementation detail (`m_source`, despite being strangely exposed as a
> public member, is not the standard interface; otherwise it would have been
> defined as such, and `source()` would have been entirely redundant.)
>
> I think I'm missing something, but what?

The point of using free functions is to allow them to be defined
separately from the classes they relate to. For example, you could have a
preexisting graph class that has differently-named members (say
"edge.start()" and "edge.end()") and wrap free functions around those
without changing the original code. However, you cannot add member
functions to a class without modifying it. Thus, Boost.Graph uses free
functions whenever possible to allow adaptation of third-party libraries
just by adding new definitions, without changing any of the existing
library code.

-- Jeremiah Willcock


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk