begin, end and directory iterators

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? - Rob.

Robert Jones 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?
Just looking at the docs, I don't think so, nor should it. There are two possible iterators constructable from a path. The directory iterator, which iterates over the files, and the path iterator, which iterators over the components of the path itself. The latter are the ones returned from some_directory.begin() and .end(), and will almost certainly be the ones returned from boost::begin/end. I would consider at making a function that returned a range of the directory iterators and using that instead. Something like: namespace bfs = boost::filesystem; boost::iterator_range< bfs::directory_iterator > \ directory_range( bfs::directory_iterator(path), bfs::directory_iterator() ); std::for_each(boost::begin(directory_range), boost::end(directory_range), f); (Untested, uncompiled, and written by someone who's rarely used range.) -- Anthony Foglia Princeton Consultants (609) 987-8787 x233

On Thu, Apr 1, 2010 at 9:44 PM, Robert Jones
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_rangeboost::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
participants (3)
-
Anthony Foglia
-
Neil Groves
-
Robert Jones