Traits and Iterators
Metaprogramming utilities that make generic BGL code possible. They are
what let a single algorithm work across adjacency_list, adjacency_matrix,
grid_graph, or a user-written graph type — each of which stores its data
differently.
You touch this section whenever you write a template function that takes a graph, or whenever you attach interior properties that need a descriptor type up front.
| Page | Why you might open it |
|---|---|
Every time you want the |
|
Only when you need to declare an interior property whose value is itself
a |
|
When you want to spell out the concrete property-map type, usually in a
function signature. Distinguishes |
|
Legacy tag-based interior properties. New code should prefer bundled properties (see Bundled Properties); the two can coexist. |
|
Graph-implementer tool. Synthesizes an |
|
Same thing for |
Why typename graph_traits<Graph>::… and not just Graph::…?
If Graph is a concrete type, both work:
using Graph = boost::adjacency_list<>;
Graph::vertex_descriptor v; // OK
boost::graph_traits<Graph>::vertex_descriptor v; // also OK
If Graph is a template parameter, you must use typename graph_traits<…>::
to disambiguate the dependent name. The graph_traits indirection also lets
third-party graph types (that don’t have the exact nested typedef by that name)
plug in via specialization.
Descriptors: what is a vertex_descriptor under the hood?
Selector (for adjacency_list) |
Concrete vertex_descriptor |
Notes |
|---|---|---|
|
|
Comparable to integers. Stable unless you call |
|
Implementation-defined pointer/iterator type |
Opaque. Do not compare to 0 or use as a vector index. Descriptors
survive |
|
Implementation-defined |
Same treatment as |
The portable sentinel for "no vertex" is graph_traits<Graph>::null_vertex()
(for vecS it happens to be std::numeric_limits<std::size_t>::max(), but
don’t rely on that).
Iterator / descriptor invalidation
| Operation | vecS |
listS |
setS / hash_setS |
|---|---|---|---|
|
iterators invalidated |
all valid |
all valid |
|
all vertex descriptors >= v invalidated, edge descriptors tracking those vertices invalidated |
descriptors |
descriptors |
|
edge iterators invalidated; out-edge iterators of |
edge iterators invalidated; out-edge iterators of |
edge iterators invalidated |
|
iterators pointing to the removed edge invalidated; others survive |
same |
same |
If you are about to mutate the graph inside a loop, copy the iterator range into a container first. See Adjacency List for the authoritative per-selector invalidation table.
Common compile errors, decoded
| Error fragment | Likely cause |
|---|---|
|
Your graph has no |
|
You used |
Template error mentioning |
You tried to write to a |
|
Unlike |
See also
-
Adjacency List — per-selector trade-offs and invalidation rules.
-
Property Maps — the user-facing guide to property maps; come here (traits) when you need the types the other guide is silent on.
-
Graph Concepts — what nested typedefs each concept requires.