r/cpp_questions • • Oct 29 '25

OPEN boost graph library example seems to run afoul of a core guideline

Consider https://www.boost.org/doc/libs/latest/libs/graph/example/dfs-example.cpp

The class is thus defined:

class dfs_time_visitor : public default_dfs_visitor
{
    typedef typename property_traits< TimeMap >::value_type T;

public:
    dfs_time_visitor(TimeMap dmap, TimeMap fmap, T& t)
    : m_dtimemap(dmap), m_ftimemap(fmap), m_time(t)
    {
    }
    template < typename Vertex, typename Graph >
    void discover_vertex(Vertex u, const Graph& g) const
    {
        put(m_dtimemap, u, m_time++);
    }
    template < typename Vertex, typename Graph >
    void finish_vertex(Vertex u, const Graph& g) const
    {
        put(m_ftimemap, u, m_time++);
    }
    TimeMap m_dtimemap;
    TimeMap m_ftimemap;
    T& m_time;//clang tidy does not like this
};

clang-tidy insists that this runs afoul of the following:

https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-const-or-ref-data-members.html

Are there any work arounds? boost graph library folks on github issues page are rather overworked. I have tried raising some issues there in the past without much success in obtaining guidance.

3 Upvotes

8 comments sorted by

17

u/ronchaine Oct 29 '25

Guidelines are guidelines, not absolute rules. There are reasons to not follow them from time to time. Since this is in 3rd party library, it might be completely reasonable to just disable the check for the file.

11

u/flyingron Oct 29 '25

Clang tidy is throwing out things that might be problems. Here, it complains that you have things that may not copy well but the class appears to be copyable. While clang thinks this construct is "rarely useful," it is here.

The class is copy-constructible. In fact that's the whole point of the DFS Visitor Concept (you did bother to read the documentation on the stuff you're getting worked up about?). It copies just fine and as intended.

The fact that it is not assignable is not a problem. It's not intended to be. In fact, the compiler will complain if you try to assign it (as the program will not be well-formed, even with clang's assinine nannybot turned off).

7

u/afforix Oct 29 '25

Just silence warnings from the system includes.

4

u/No-Dentist-1645 Oct 29 '25

Yeah, that's a good guideline, but as any "guideline", there are exceptions that can justify not applying them. We don't know why the Boost developer decided to use a raw reference here, it could be that they designed the class such that it can't (or doesn't make sense to) copy-assign them, and since Boost targets a very wide range of platforms and compilers, it could be that something like gsl::not_null or std::reference_wrapper either doesn't work in some of them, or has a minute (but existing) performance impact.

If you really wanted to know why this is the case, you should ask the boost community directly. Otherwise, we can only speculate, but we can't objectively state it is "bad design" or anything. It's usually recommended to just disable tools like clang-tidy and such on your external includes directories.

2

u/Foxi_Foxa Aug 27 '26

Hey ! 😄

I'm back here after a few months to let you know we began supporting stateful visitors in Boost.Graph algorithms, which should solve your complaint (let us know if this isn't the case)

The design is to define state as regular members in the visitor:

struct order_recorder : boost::graph::default_mas_visitor {
    std::vector<vertex_descriptor> order;
    void finish_vertex(vertex_descriptor u, const Graph&) { order.push_back(u); }
};

and pass the visitor wrapped in a `std::reference_wrapper` if statefulness is required:

order_recorder visitor;
boost::graph::maximum_adjacency_search(g, weight_map, std::ref(visitor));

This should naturally deprecate the need for reference/pointer members, and align better with core guidelines, without breaking anything.

To test the design and allow room for mistakes, so far only two obscure algorithms have been updated this way, like maximum_adjacency_search , but my current development efforts are oriented towards extending this support to other algorithms including DFS. Complete support for every BGL algorithm probably won't land in 1.93 but let's try 😜

You can look at the documentation here with the code examples showing the stateful support and let us know if you'd be happier that way : https://www.boost.org/doc/libs/develop/libs/graph/doc/html/graph/algorithms/utility/maximum_adjacency_search.html#_example_2

1

u/Foxi_Foxa May 13 '26

Hi !

I agree with your general feeling, thank you for raising it up ❤️

One thing worth knowing is the historical context: this example was written ~26 years ago: at that time what we consider modern guidelines did not even exist. Quickly looking at its git history, it was updated in 2016 to upgrade type aliases `typedef` to `using`, then someone ran clang-format on it in 2019.

We are currently gathering user feedbacks to revitalize the Boost Graph Library, beginning with its documentation and examples. A new documentation website with entirely modern code examples is available on this local fork and will soon become a PR : https://becheler.github.io/graph/graph/index.html

You are welcome to join the discussion about this new doc and suggest some way to make it better: https://github.com/boostorg/graph/discussions/478

Although I haven't run clang tidy on the examples, it may be a good idea to do so. I can not guarantee it will be the case: guidelines are opinionated and systematic flagging risks adding one more constraint on the BGL open-source maintainers 😉

On a side note, I would love if you could point me to the issues you raised in the past, I will try my best to answer them 😄

See you around!

0

u/[deleted] Oct 29 '25

[deleted] — view deleted comment

2

u/alfps Oct 29 '25

I agree.

It's hard to think of any situation where it might actually help. While it does got in the way for valid uses such as this.