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
commit 49003a7c6896980c797d7fa2f404a2620f8ed389
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Tue May 10 13:09:22 2016 +0000
Optimize __directory_iterator_proxy for the common case
* include/experimental/bits/fs_dir.h (__directory_iterator_proxy):
Overload operator* to move from rvalues.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@236078 138bc75d-0d04-0410-961f-82ee72b054a4
diff --git a/libstdc++-v3/include/experimental/bits/fs_dir.h b/libstdc++-v3/include/experimental/bits/fs_dir.h
index 5fd41c2..011d398 100644
--- a/libstdc++-v3/include/experimental/bits/fs_dir.h
+++ b/libstdc++-v3/include/experimental/bits/fs_dir.h
@@ -158,7 +158,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
struct __directory_iterator_proxy
{
- const directory_entry& operator*() const noexcept { return _M_entry; }
+ const directory_entry& operator*() const& noexcept { return _M_entry; }
+
+ directory_entry operator*() && noexcept { return std::move(_M_entry); }
private:
friend class directory_iterator;