diff mbox series

LWG 2989 hide path iostream operators from normal lookup

Message ID 20180618185937.GA21722@redhat.com
State New
Headers show
Series LWG 2989 hide path iostream operators from normal lookup | expand

Commit Message

Jonathan Wakely June 18, 2018, 6:59 p.m. UTC
By only defining these operators as friends (with no namespace-scope
declaration) they can only be found by ADL and do not participate in
overload resolution for arguments of types other than path.

	LWG 2989 hide path iostream operators from normal lookup
	* include/bits/fs_path.h (operator<<, operator>>): Define inline as
	friends.
	* testsuite/27_io/filesystem/path/io/dr2989.cc: New.

Tested powerpc64le-linux, commmitted to trunk.
commit 51827611e8aa67b983664bf566d8947bf9a4359a
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Jun 18 19:07:16 2018 +0100

    LWG 2989 hide path iostream operators from normal lookup
    
    By only defining these operators as friends (with no namespace-scope
    declaration) they can only be found by ADL and do not participate in
    overload resolution for arguments of types other than path.
    
            LWG 2989 hide path iostream operators from normal lookup
            * include/bits/fs_path.h (operator<<, operator>>): Define inline as
            friends.
            * testsuite/27_io/filesystem/path/io/dr2989.cc: New.
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/fs_path.h b/libstdc++-v3/include/bits/fs_path.h
index 6eab800ac56..e3938d06d59 100644
--- a/libstdc++-v3/include/bits/fs_path.h
+++ b/libstdc++-v3/include/bits/fs_path.h
@@ -363,6 +363,26 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
     iterator begin() const;
     iterator end() const;
 
+    /// Write a path to a stream
+    template<typename _CharT, typename _Traits>
+      friend std::basic_ostream<_CharT, _Traits>&
+      operator<<(std::basic_ostream<_CharT, _Traits>& __os, const path& __p)
+      {
+	__os << std::quoted(__p.string<_CharT, _Traits>());
+	return __os;
+      }
+
+    /// Read a path from a stream
+    template<typename _CharT, typename _Traits>
+      friend std::basic_istream<_CharT, _Traits>&
+      operator>>(std::basic_istream<_CharT, _Traits>& __is, path& __p)
+      {
+	std::basic_string<_CharT, _Traits> __tmp;
+	if (__is >> std::quoted(__tmp))
+	  __p = std::move(__tmp);
+	return __is;
+      }
+
     // Create a basic_string by reading until a null character.
     template<typename _InputIterator,
 	     typename _Traits = std::iterator_traits<_InputIterator>,
@@ -505,26 +525,6 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
     return __result;
   }
 
-  /// Write a path to a stream
-  template<typename _CharT, typename _Traits>
-    basic_ostream<_CharT, _Traits>&
-    operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p)
-    {
-      __os << std::quoted(__p.string<_CharT, _Traits>());
-      return __os;
-    }
-
-  /// Read a path from a stream
-  template<typename _CharT, typename _Traits>
-    basic_istream<_CharT, _Traits>&
-    operator>>(basic_istream<_CharT, _Traits>& __is, path& __p)
-    {
-      basic_string<_CharT, _Traits> __tmp;
-      if (__is >> std::quoted(__tmp))
-	__p = std::move(__tmp);
-      return __is;
-    }
-
   template<typename _InputIterator>
     inline auto
     u8path(_InputIterator __first, _InputIterator __last)
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/io/dr2989.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/io/dr2989.cc
new file mode 100644
index 00000000000..b9a1235e1fe
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/io/dr2989.cc
@@ -0,0 +1,35 @@ 
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++17 } }
+
+#include <iostream>
+#include <filesystem>
+
+using namespace std::filesystem;
+
+struct P {
+  operator path&();
+};
+
+void foo(std::iostream& s) {
+  P p;
+  s << p; // { dg-error "no match" }
+  s >> p; // { dg-error "no match" }
+}
+// { dg-prune-output "no type .* std::enable_if" }