|
Boost : |
Subject: Re: [boost] [filesystem] C++11 range-based for statement and BOOST_FOREACH support
From: Beman Dawes (bdawes_at_[hidden])
Date: 2012-07-20 14:54:13
On Sat, Jul 14, 2012 at 12:25 PM, Nathan Ridge <zeratul976_at_[hidden]> wrote:
>
>> > Hm.
>> >
>> > Why not follow this design:
>> >
>> > http://www.boost.org/doc/libs/1_50_0/libs/range/doc/html/range/reference/ranges/istream_range.html
>>
>> The implementation would be more elegant, but the user would have to write:
>>
>> BOOST_FOREACH(directory_entry& x, directory_range(directory_iterator(".")))
>>
>> instead of the simpler:
>>
>> BOOST_FOREACH(directory_entry& x, directory_iterator("."))
>>
>> I verified the above with an actual implementation and test.
>
> I don't see why that's the case. In the posted link, the function istream_range()
> takes an argument of type istream&, which is the same argument type that the
> constructor of istream_iterator takes. Notice that istream_range() does *not*
> take an actual istream_iterator as its constructor argument.
>
> Similarly, I would expect directory_range() to take a path argument, and return
> something like make_iterator_range(directory_iterator(p), directory_iterator()).
The make_iterator_range template requires ForwardIterators, but
directory_iterator only meets InputIterator requirements. Perhaps
someone familiar with Boost.Range can come up with a workaround.
For C++11 range-based for statement use, this works:
class directory_range
{
public:
directory_range(const path& p) : m_iter(p) {}
directory_iterator begin() const { return m_iter; }
directory_iterator end() const { return directory_iterator(); }
private:
directory_iterator m_iter;
};
I'll ask the C++ committee's Filesystem Study Group if they prefer the
above over the non-member begin()/end() approach. It is a bit less
efficient, although that's probably swamped by operating system
overhead.
Thanks,
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk