[SG16-Unicode] [isocpp-lib-ext] Proposed design change to P1030 filesystem::path_view

Niall Douglas s_sourceforge at nedprod.com
Tue Aug 27 13:53:27 CEST 2019


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) {
         use(path);
     }

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

Niall


More information about the Unicode mailing list