From dd4a309e05dc2ccde33e8e639aa063dc0c3b176f Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 5 Apr 2019 17:56:31 +0100 Subject: [PATCH] Use hidden friends for directory iterator comparisons The equality operators for directory iterators are not explicitly specified in the standard, they're only required to meet the iterator requirements. This means we don't need to declare them at namespace scope and can implement them as hidden friends. Also add 'noexcept' to directory_iterator's dereference operators. * include/bits/fs_dir.h (directory_iterator::operator*) (directory_iterator::operator->): Add noexcept. (operator==, operator!=): Replace namespace-scope equality operators for directory iterators with hidden friends. From-SVN: r270175 --- libstdc++-v3/ChangeLog | 5 +++ libstdc++-v3/include/bits/fs_dir.h | 50 +++++++++++++----------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 655c3d073a2..65b16d8e68b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2019-04-05 Jonathan Wakely + * include/bits/fs_dir.h (directory_iterator::operator*) + (directory_iterator::operator->): Add noexcept. + (operator==, operator!=): Replace namespace-scope equality operators + for directory iterators with hidden friends. + PR libstdc++/89986 * config/abi/pre/gnu.ver: Add missing exports. * testsuite/27_io/filesystem/iterators/directory_iterator.cc: Test diff --git a/libstdc++-v3/include/bits/fs_dir.h b/libstdc++-v3/include/bits/fs_dir.h index a5947b39541..69f0eb825fe 100644 --- a/libstdc++-v3/include/bits/fs_dir.h +++ b/libstdc++-v3/include/bits/fs_dir.h @@ -390,8 +390,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 directory_iterator& operator=(directory_iterator&& __rhs) noexcept = default; - const directory_entry& operator*() const; - const directory_entry* operator->() const { return &**this; } + const directory_entry& operator*() const noexcept; + const directory_entry* operator->() const noexcept { return &**this; } directory_iterator& operator++(); directory_iterator& increment(error_code& __ec); @@ -407,7 +407,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 friend bool operator==(const directory_iterator& __lhs, - const directory_iterator& __rhs); + const directory_iterator& __rhs) noexcept + { + return !__rhs._M_dir.owner_before(__lhs._M_dir) + && !__lhs._M_dir.owner_before(__rhs._M_dir); + } + + friend bool + operator!=(const directory_iterator& __lhs, + const directory_iterator& __rhs) noexcept + { return !(__lhs == __rhs); } friend class recursive_directory_iterator; @@ -422,17 +431,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 end(directory_iterator) noexcept { return directory_iterator(); } - inline bool - operator==(const directory_iterator& __lhs, const directory_iterator& __rhs) - { - return !__rhs._M_dir.owner_before(__lhs._M_dir) - && !__lhs._M_dir.owner_before(__rhs._M_dir); - } - - inline bool - operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs) - { return !(__lhs == __rhs); } - class recursive_directory_iterator { public: @@ -499,7 +497,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 friend bool operator==(const recursive_directory_iterator& __lhs, - const recursive_directory_iterator& __rhs); + const recursive_directory_iterator& __rhs) noexcept + { + return !__rhs._M_dirs.owner_before(__lhs._M_dirs) + && !__lhs._M_dirs.owner_before(__rhs._M_dirs); + } + + friend bool + operator!=(const recursive_directory_iterator& __lhs, + const recursive_directory_iterator& __rhs) noexcept + { return !(__lhs == __rhs); } struct _Dir_stack; std::__shared_ptr<_Dir_stack> _M_dirs; @@ -513,19 +520,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 end(recursive_directory_iterator) noexcept { return recursive_directory_iterator(); } - inline bool - operator==(const recursive_directory_iterator& __lhs, - const recursive_directory_iterator& __rhs) - { - return !__rhs._M_dirs.owner_before(__lhs._M_dirs) - && !__lhs._M_dirs.owner_before(__rhs._M_dirs); - } - - inline bool - operator!=(const recursive_directory_iterator& __lhs, - const recursive_directory_iterator& __rhs) - { return !(__lhs == __rhs); } - _GLIBCXX_END_NAMESPACE_CXX11 // @} group filesystem -- 2.30.2