Boost logo

Boost :

Subject: Re: [boost] [graph] ranges used by boost.graph not compatible with c++11 range-based-for
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2013-10-02 16:51:49


On Wed, 2 Oct 2013, Gabriel Redner wrote:

> I noticed today that code of this form does not compile:
>
> typedef boost::adjacency_list<boost::vecS, boost::vecS,
> boost::bidirectionalS, Node, Edge> Graph;
> typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
> typedef boost::graph_traits<Graph>::edge_descriptor Edge;
> Graph graph = ...;
> Vertex vertex = ...;
> for (Edge edge: boost::out_edges(vertex, graph)) {...}
>
> The reason is that out_edges returns a std::pair of iterators. This
> is a valid range in the sense of boost.range, and so is compatible
> with BOOST_FOREACH. However, this is not a valid range in the sense
> of c++11 range-based-for.

It had been allowed in range-based for previously, but was removed before
C++11 was released.

> It's not clear whether this can easily be changed. In a perfect world
> these functions should probably return something like
> boost::iterator_range which would be compatible with both boost- and
> language-level concepts of range-ness. However, the graph
> documentation explicitly names the return types of out_edges et. al.
> (see [1]) as std::pairs, which eliminates some wiggle room. Is there
> an alternate way to address this?

I'm not sure. There might be a way to inherit from std::pair to be
compatible while still allowing range-based for, but I am not sure.
Another option would be a wrapper that converted an std::pair to a range.

-- Jeremiah Willcock


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