C++ Logo


Advanced search

Subject: Re: [SG16-Unicode] [isocpp-lib-ext] Proposed design change to P1030 filesystem::path_view
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2019-08-27 06:53:27

On 26/08/2019 22:32, David Stone wrote:
> I have several times written (and seen written) code like
>     auto directory = std::filesystem::path(...);
>     for (auto const & path : directory) {
>         use(path);
>     }
> when what I meant was
>     auto directory = std::filesystem::path(...);
>     for (auto path_it = std::filesystem::directory_iterator(directory);
> path_it != std::filesystem::directory_iterator(); ++path_it) {
>         use(*path_it);
>     }

I'd have some sympathy for you if you had written:

     auto directory = std::filesystem::path(...);
     for (auto const & path : *directory) {

... and you did not get directory enumeration, as it would be reasonable
to expect dereferencing a path might do something like contents enumeration.

But if you iterate a container of X, you expect to yield X. I don't know
why you'd expect it to yield Y instead without performing some sort of
additional operation, like indirection, or a call operator, or *anything*.

Incidentally, directory_iterator is an abomination, and it should get
hard-deprecated without Annex D as soon as we have an alternative. Not
only is it slow, it's bug prone, unreliable, and insecure. Awful.


SG16 list run by sg16-owner@lists.isocpp.org