Opened 11 years ago

Last modified 5 years ago

#6521 assigned Feature Requests

Directory listing using C++11 range-based for loops

Reported by: mustrumr97@… Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.48.0 Severity: Cosmetic
Keywords: Cc:

Description

I have written a simple 'container' class for a directory, 'containing' all of its subdirectories:

namespace boost{
    namespace filesystem{
        class directory{
            path p_;
            public:
                inline directory(path p):p_(p){
                    return;
                }
                directory_iterator begin(){
                    return directory_iterator(p_);
                }
                directory_iterator end(){
                    return directory_iterator();
                }
        };
    }
}

so that that range-based for loop can be used:

for(auto i:directory("/home")){
    ...
}

Change History (3)

comment:1 by Beman Dawes, 11 years ago

Status: newassigned

A message has been posted to the Boost developers mailing list asking for comments on your proposal.

Thanks for raising the issue,

--Beman

comment:2 by mustrumr97@…, 10 years ago

I have read the mailing list. I think all filtering should be moved to the directory iterators and that the helper functions should only be used for passing parameters to the iterator constructor like this:

template<class... argts>
boost::iterator_range<directory_iterator>
directory_range(argts... args){
    return boost::iterator_range<directory_iterator>(
        directory_iterator(args...),
        directory_iterator()
    );
}
template<class... argts>
boost::iterator_range<recursive_directory_iterator>
recursive_directory_range(argts... args){
    return boost::iterator_range<recursive_directory_iterator(
        recursive_directory_iterator(args...),
        recursive_directory_iterator()
    );
}

comment:3 by mlimber, 5 years ago

Note that because directory_iterator supports begin()/end(), one can already use range-for loops like this:

for( const auto& dir_ent : directory_iterator{ "my/path/to/iterate" } )
{
    cout << dir_ent.path() << '\n';
}
Note: See TracTickets for help on using tickets.