Boost logo

Boost Users :

Subject: Re: [Boost-users] begin, end and directory iterators
From: Anthony Foglia (AFoglia_at_[hidden])
Date: 2010-04-02 10:11:17


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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net