[PATCH] libstdc++/71005 fix post-increment for filesystem iterators

Jonathan Wakely jwakely@redhat.com
Tue May 10 13:13:00 GMT 2016


On 10/05/16 12:25 +0100, Jonathan Wakely wrote:
>Eric pointed out that the post-increment operator on filesystem
>directory iterators was returning the incremented value, because the
>returnedobject shared its state with the incremented iterator.
>
>This makes post-increment return a proxy, which contains the
>directory_entry value before the increment.
>
>	PR libstdc++/71005
>	* include/experimental/bits/fs_dir.h (__directory_iterator_proxy):
>	New type.
>	(directory_iterator::operator++(int)): Return proxy.
>	(recursive_directory_iterator::operator++(int)): Likewise.
>	* testsuite/experimental/filesystem/iterators/directory_iterator.cc:
>	Test post-increment.
>	* testsuite/experimental/filesystem/iterators/
>	recursive_directory_iterator.cc: Likewise.
>
>Tested x86_64-linux, committed to trunk. Backports to follow.

After discussion with Eric we came up with this optimization, so that
the common case of do_something(*it++) can move the directory_entry
out of the proxy instead of copying it.

Overloading on ref-qualifiers allows this (IIUC invalid) case to still
DTRT:

    auto it2 = it++;   // it2 is actually a proxy, not an iterator
    auto ent1 = *it2;
    auto ent2 = *it2;  // doesn't access moved-from value

-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 1058 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20160510/88c8f31c/attachment.bin>


More information about the Libstdc++ mailing list