Use hidden friends for directory iterator comparisons
authorJonathan Wakely <jwakely@redhat.com>
Fri, 5 Apr 2019 16:56:31 +0000 (17:56 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 5 Apr 2019 16:56:31 +0000 (17:56 +0100)
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
libstdc++-v3/include/bits/fs_dir.h

index 655c3d073a209e5b3c340910e5b9a9f3ccab7370..65b16d8e68be6e8824dfbeb21a9c035ee6848cf6 100644 (file)
@@ -1,5 +1,10 @@
 2019-04-05  Jonathan Wakely  <jwakely@redhat.com>
 
+       * 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
index a5947b39541b994b08e657b379e67cfcc16a6cbd..69f0eb825fe7d264438aba7ebe5ad21fe35c71a5 100644 (file)
@@ -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