[committed] libstdc++: Define net::socket_base::message_flags operators as friends [PR 100285]

Jonathan Wakely jwakely@redhat.com
Tue Apr 27 13:10:09 GMT 2021


The overloaded operators for socket_base::message_flags should only be
defined when the message_flags type itself is defined. Rather than
duplicate the preprocessor conditional, this moves the operators into
the same scope as the type, defining them as hidden friends.

As well as fixing the bug, this has all the usual advantages of hidden
friends (they are not visible to normal name lookup for unrelated
types).

For consistency, do the same for the resolver_base::flags bitmask
operators too.

libstdc++-v3/ChangeLog:

	PR libstdc++/100285
	* include/experimental/internet (resolver_base::flags):
	Define overloaded operators as hidden friends.
	* include/experimental/socket (socket_base::message_flags):
	Likewise.

Tested x86_64-linux. Committed to trunk.

-------------- next part --------------
commit 8aadb4fedb17e7a44583a7a5a4e96b1874e1ce98
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Apr 27 11:07:47 2021

    libstdc++: Define net::socket_base::message_flags operators as friends [PR 100285]
    
    The overloaded operators for socket_base::message_flags should only be
    defined when the message_flags type itself is defined. Rather than
    duplicate the preprocessor conditional, this moves the operators into
    the same scope as the type, defining them as hidden friends.
    
    As well as fixing the bug, this has all the usual advantages of hidden
    friends (they are not visible to normal name lookup for unrelated
    types).
    
    For consistency, do the same for the resolver_base::flags bitmask
    operators too.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100285
            * include/experimental/internet (resolver_base::flags):
            Define overloaded operators as hidden friends.
            * include/experimental/socket (socket_base::message_flags):
            Likewise.

diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet
index 2e3fd06ead2..11961762880 100644
--- a/libstdc++-v3/include/experimental/internet
+++ b/libstdc++-v3/include/experimental/internet
@@ -1660,39 +1660,39 @@ namespace ip
     static constexpr flags all_matching		= (flags)AI_ALL;
     static constexpr flags address_configured	= (flags)AI_ADDRCONFIG;
 
+    friend constexpr flags
+    operator&(flags __f1, flags __f2) noexcept
+    { return flags( int(__f1) & int(__f2) ); }
+
+    friend constexpr flags
+    operator|(flags __f1, flags __f2) noexcept
+    { return flags( int(__f1) | int(__f2) ); }
+
+    friend constexpr flags
+    operator^(flags __f1, flags __f2) noexcept
+    { return flags( int(__f1) ^ int(__f2) ); }
+
+    friend constexpr flags
+    operator~(flags __f) noexcept
+    { return flags( ~int(__f) ); }
+
+    friend constexpr flags&
+    operator&=(flags& __f1, flags __f2) noexcept
+    { return __f1 = (__f1 & __f2); }
+
+    friend constexpr flags&
+    operator|=(flags& __f1, flags __f2) noexcept
+    { return __f1 = (__f1 | __f2); }
+
+    friend constexpr flags&
+    operator^=(flags& __f1, flags __f2) noexcept
+    { return __f1 = (__f1 ^ __f2); }
+
   protected:
     resolver_base() = default;
     ~resolver_base() = default;
   };
 
-  constexpr resolver_base::flags
-  operator&(resolver_base::flags __f1, resolver_base::flags __f2) noexcept
-  { return resolver_base::flags( int(__f1) & int(__f2) ); }
-
-  constexpr resolver_base::flags
-  operator|(resolver_base::flags __f1, resolver_base::flags __f2) noexcept
-  { return resolver_base::flags( int(__f1) | int(__f2) ); }
-
-  constexpr resolver_base::flags
-  operator^(resolver_base::flags __f1, resolver_base::flags __f2) noexcept
-  { return resolver_base::flags( int(__f1) ^ int(__f2) ); }
-
-  constexpr resolver_base::flags
-  operator~(resolver_base::flags __f) noexcept
-  { return resolver_base::flags( ~int(__f) ); }
-
-  constexpr resolver_base::flags&
-  operator&=(resolver_base::flags& __f1, resolver_base::flags __f2) noexcept
-  { return __f1 = (__f1 & __f2); }
-
-  constexpr resolver_base::flags&
-  operator|=(resolver_base::flags& __f1, resolver_base::flags __f2) noexcept
-  { return __f1 = (__f1 | __f2); }
-
-  constexpr resolver_base::flags&
-  operator^=(resolver_base::flags& __f1, resolver_base::flags __f2) noexcept
-  { return __f1 = (__f1 ^ __f2); }
-
   // TODO define resolver_base::flags static constants in .so for C++14 mode
 
   /// @}
diff --git a/libstdc++-v3/include/experimental/socket b/libstdc++-v3/include/experimental/socket
index 18849f607f8..bf6a8c87e67 100644
--- a/libstdc++-v3/include/experimental/socket
+++ b/libstdc++-v3/include/experimental/socket
@@ -319,13 +319,38 @@ inline namespace v1
       = (message_flags)MSG_DONTROUTE;
 
     static const int max_listen_connections = SOMAXCONN;
-#endif
+
+    // message_flags bitmask operations are defined as hidden friends.
+
+    friend constexpr message_flags
+    operator&(message_flags __f1, message_flags __f2) noexcept
+    { return message_flags( int(__f1) & int(__f2) ); }
+
+    friend constexpr message_flags
+    operator|(message_flags __f1, message_flags __f2) noexcept
+    { return message_flags( int(__f1) | int(__f2) ); }
+
+    friend constexpr message_flags
+    operator^(message_flags __f1, message_flags __f2) noexcept
+    { return message_flags( int(__f1) ^ int(__f2) ); }
+
+    friend constexpr message_flags
+    operator~(message_flags __f) noexcept
+    { return message_flags( ~int(__f) ); }
+
+    friend constexpr message_flags&
+    operator&=(message_flags& __f1, message_flags __f2) noexcept
+    { return __f1 = (__f1 & __f2); }
+
+    friend constexpr message_flags&
+    operator|=(message_flags& __f1, message_flags __f2) noexcept
+    { return __f1 = (__f1 | __f2); }
+
+    friend constexpr message_flags&
+    operator^=(message_flags& __f1, message_flags __f2) noexcept
+    { return __f1 = (__f1 ^ __f2); }
 
   protected:
-    socket_base() = default;
-    ~socket_base() = default;
-
-#ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
     struct __msg_hdr : ::msghdr
     {
 #ifdef IOV_MAX
@@ -382,43 +407,12 @@ inline namespace v1
 	}
     };
 #endif
+
+  protected:
+    socket_base() = default;
+    ~socket_base() = default;
   };
 
-  constexpr socket_base::message_flags
-  operator&(socket_base::message_flags __f1, socket_base::message_flags __f2)
-    noexcept
-  { return socket_base::message_flags( int(__f1) & int(__f2) ); }
-
-  constexpr socket_base::message_flags
-  operator|(socket_base::message_flags __f1, socket_base::message_flags __f2)
-    noexcept
-  { return socket_base::message_flags( int(__f1) | int(__f2) ); }
-
-  constexpr socket_base::message_flags
-  operator^(socket_base::message_flags __f1, socket_base::message_flags __f2)
-    noexcept
-  { return socket_base::message_flags( int(__f1) ^ int(__f2) ); }
-
-  constexpr socket_base::message_flags
-  operator~(socket_base::message_flags __f)
-    noexcept
-  { return socket_base::message_flags( ~int(__f) ); }
-
-  constexpr socket_base::message_flags&
-  operator&=(socket_base::message_flags& __f1, socket_base::message_flags __f2)
-    noexcept
-  { return __f1 = (__f1 & __f2); }
-
-  constexpr socket_base::message_flags&
-  operator|=(socket_base::message_flags& __f1, socket_base::message_flags __f2)
-    noexcept
-  { return __f1 = (__f1 | __f2); }
-
-  constexpr socket_base::message_flags&
-  operator^=(socket_base::message_flags& __f1, socket_base::message_flags __f2)
-    noexcept
-  { return __f1 = (__f1 ^ __f2); }
-
   // TODO define socket_base static constants in .so for C++14 mode
 
 #if _GLIBCXX_HAVE_UNISTD_H


More information about the Libstdc++ mailing list