r/cpp • • 14h ago

Boost.Graph 1.93: pass stateful visitors with std::ref

Dear Boost.Graph community,

Starting with Boost 1.93, you will be able to pass stateful visitors with std::ref :

    Counter vis;
    boost::depth_first_search(g, boost::visitor(std::ref(vis)));
    // vis.count is now up to date

For context, BGL visitors are small structures with event methods that are fired at specific points of an algorithm (e.g. the DFS algorithm calling vis.start_vertex(vertex, graph) at a certain point). Most visitors are meant to be pure observers and their events return void so they can't influence the execution flow of their algorithm. If a visitor only prints data, stateless is fine. But when wanting an answer out of a traversal (a yes/no, timestamps, paths...), it has to accumulate state.

Historically, algorithms implementation (circa 2000) predate std::ref (C++11). Any state stored in the visitor was therefore updated on a copy and lost. The recommended approach was to capture state through pointer or reference. But a reference member contradicts Core Guideline C.12 and triggers clang-tidy's cppcoreguidelines-avoid-const-or-ref-data-members, as reported by a redditor: https://www.reddit.com/r/cpp_questions/comments/1oj6mi6/boost_graph_library_example_seems_to_run_afoul_of/

From 1.93, algorithms will be able to unwrap visitors when the caller passed a reference_wrapper. It's an extension, you don't need to modify your existing code if it works. But if you want to silence the clang-tidy once for all, you now can 😄

Four exceptions to this: we had 4 planar algorithms that surprisingly took visitors by reference, against this convention, and needed to be deprecated to harmonize their signatures:

- planar_face_traversal,
- make_connected,
- make_biconnected_planar
- make_maximal_planar

In 1.93, your calls still compile and behave the same, but they now emit a deprecation warning, and the old overloads are planned for removal in Boost 1.95. The replacements live in boost::graph namespace (we are harmonizing namespaces progressively too)

boost::make_connected(g, vm, vis);  // still works, deprecated
boost::graph::make_connected(g, vm, std::ref(vis));  // replacement

All details, explanations and rational live in the dedicated Github Discussion: https://github.com/boostorg/graph/discussions/700

See you there ! 🥳

24 Upvotes

2 comments sorted by

4

u/obsidian_golem 13h ago

Why not make all the functions take a forwarding reference instead?

6

u/Foxi_Foxa 11h ago

The idea was studied and discarded for a bunch of reasons.

  1. Consistency with the standard algorithms, where functors are taken by value, and std::ref usable when references are required.
  2. It would have been a silent behavior change, possibly breaking, from "the algo works on a copy" to "the algo mutates my object" without diagnostic, particularly when visitors are reused across multiple algorithms calls. Const visitors would have stopped compiling.
  3. An unconstrained forwarding reference is greedy in overload resolution, which causes issues, and also it would not have reached the named parameters interfaces, that store visitors by copy.
  4. It would have required modifying a large number of public signatures and documentation pages, with a long deprecation notice, without guarantee of not breaking user code.

Compared with that, accepting a reference wrapper is purely additive: existing code is untouched and sharing state is opt-in 😄