On Thu, Apr 1, 2010 at 9:44 PM, Robert Jones <robertgbjones@gmail.com> wrote:
Do boost::begin() and boost::end() support directory iterators? ie., can you write

void f(boost::filesystem::path&);

boost::filesystem::path some_directory;

std::for_each(boost::begin(some_directory), boost::end(some_directory), f);

If they don't, could they and should they?


I don't believe the code would do what you want. The boost::filesystem::path is a model of a Range but it is a range of strings that are a list of the portions of the path.

With the newer version of Boost.Range on the trunk it is possible to write:
boost::for_each(some_directory, f);
but f would be called with each string component of the path.

However, I am very enthusiastic about the idea of a helper function that returns a boost::iterator_range<boost::filesystem::directory_iterator> and perhaps another that returns the recursive version too.

The Boost.Range library provides the mechanisms required to allow iterator based algorithms to be wrapped to provide a range interface. This in my opinion, is generally a good idea. The example you have shown also shows that it might be wise to consider a factory function to generate an iterator range from iterator types.

Therefore for your example, once the range factory function were implemented I would expect to be able write:
boost::for_each( boost::filesystem::directory_elements(some_directory), f );
 
If this suggestion fits your use-case then please add a feature request for the Boost.Filesystem component, or email me directly and I'll be happy to put a proposal together and see if this idea is attractive to the Boost.Filesystem maintainer.

Regards,
Neil Groves