Patchwork std::bind vs ::bind ambiguity

login
register
mail settings
Submitter Jonathan Wakely
Date May 3, 2011, 12:03 a.m.
Message ID <BANLkTikkOMiy_Hr8AeBdpW0rQJCB-fqdeQ@mail.gmail.com>
Download mbox | patch
Permalink /patch/93733/
State New
Headers show

Comments

Jonathan Wakely - May 3, 2011, 12:03 a.m.
On 25 April 2011 19:11, Jonathan Wakely wrote:
>
> Here's a patch which removes std::bind from the overload set when the
> first argument is "socketlike" which I've defined as is_integral ||
> is_enum.  I considered using is_convertible to int to detect
> "socketlike" types, but a callable class type could be a valid
> argument to std::bind and also have implicit conversion to int.
> Another option is to constrain std::bind to function
> pointers/references, pointers to members, class types and pointers or
> references to class types ... but that's quite a lengthy list!
>
> The patch also renames the template parameters Functor to Func (in my
> book "functor" means a callable class, but the parameter could be a
> function pointer instead) and ArgTypes to BoundArgs (to match the name
> in the standard.)
>

The version I've checked in is attached. Compared to the previous
patch this removes the definition of the static const member, which as
Paolo pointed out is not needed.

2011-05-03  Jonathan Wakely  <jwakely.gcc@gmail.com>

        * include/std/functional (bind): Remove from overload set when first
        argument type might be a socket file descriptor.
        * testsuite/20_util/bind/socket.cc: New.

Tested x86_64-linux and committed to trunk

Patch

Index: include/std/functional
===================================================================
--- include/std/functional	(revision 173271)
+++ include/std/functional	(working copy)
@@ -1422,39 +1422,58 @@ 
     struct is_bind_expression<_Bind_result<_Result, _Signature> >
     : public true_type { };
 
-  template<typename _Functor, typename... _ArgTypes>
+  // Trait type used to remove std::bind() from overload set via SFINAE
+  // when first argument has integer type, so that std::bind() will
+  // not be a better match than ::bind() from the BSD Sockets API.
+  template<typename _Tp>
+    class __is_socketlike
+    {
+      typedef typename decay<_Tp>::type _Tp2;
+    public:
+      static const bool value =
+	is_integral<_Tp2>::value || is_enum<_Tp2>::value;
+    };
+
+  template<bool _SocketLike, typename _Func, typename... _BoundArgs>
     struct _Bind_helper
     {
-      typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
+      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
 	__maybe_type;
-      typedef typename __maybe_type::type __functor_type;
-      typedef _Bind<__functor_type(typename decay<_ArgTypes>::type...)> type;
+      typedef typename __maybe_type::type __func_type;
+      typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
     };
 
+  // Partial specialization for is_socketlike == true, does not define
+  // nested type so std::bind() will not participate in overload resolution
+  // when the first argument might be a socket file descriptor.
+  template<typename _Func, typename... _BoundArgs>
+    struct _Bind_helper<true, _Func, _BoundArgs...>
+    { };
+
   /**
    *  @brief Function template for std::bind.
    *  @ingroup binders
    */
-  template<typename _Functor, typename... _ArgTypes>
-    inline
-    typename _Bind_helper<_Functor, _ArgTypes...>::type
-    bind(_Functor&& __f, _ArgTypes&&... __args)
+  template<typename _Func, typename... _BoundArgs>
+    inline typename
+    _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
+    bind(_Func&& __f, _BoundArgs&&... __args)
     {
-      typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
+      typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
       typedef typename __helper_type::__maybe_type __maybe_type;
       typedef typename __helper_type::type __result_type;
-      return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
-			   std::forward<_ArgTypes>(__args)...);
+      return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
+			   std::forward<_BoundArgs>(__args)...);
     }
 
-  template<typename _Result, typename _Functor, typename... _ArgTypes>
+  template<typename _Result, typename _Func, typename... _BoundArgs>
     struct _Bindres_helper
     {
-      typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
+      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
 	__maybe_type;
       typedef typename __maybe_type::type __functor_type;
       typedef _Bind_result<_Result,
-			   __functor_type(typename decay<_ArgTypes>::type...)>
+			   __functor_type(typename decay<_BoundArgs>::type...)>
 	type;
     };
 
@@ -1462,16 +1481,16 @@ 
    *  @brief Function template for std::bind<R>.
    *  @ingroup binders
    */
-  template<typename _Result, typename _Functor, typename... _ArgTypes>
+  template<typename _Result, typename _Func, typename... _BoundArgs>
     inline
-    typename _Bindres_helper<_Result, _Functor, _ArgTypes...>::type
-    bind(_Functor&& __f, _ArgTypes&&... __args)
+    typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
+    bind(_Func&& __f, _BoundArgs&&... __args)
     {
-      typedef _Bindres_helper<_Result, _Functor, _ArgTypes...> __helper_type;
+      typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
       typedef typename __helper_type::__maybe_type __maybe_type;
       typedef typename __helper_type::type __result_type;
-      return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
-			   std::forward<_ArgTypes>(__args)...);
+      return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
+			   std::forward<_BoundArgs>(__args)...);
     }
 
   /**
Index: testsuite/20_util/bind/socket.cc
===================================================================
--- testsuite/20_util/bind/socket.cc	(revision 0)
+++ testsuite/20_util/bind/socket.cc	(revision 0)
@@ -0,0 +1,41 @@ 
+// Copyright (C) 2011 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/>.
+
+// 20.8.9 Function template bind
+
+// Verify that calls to bind() in BSD sockets API do not match std::bind()
+// (this is a GNU extension)
+
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+#include <functional>
+
+struct my_sockaddr { };
+typedef long my_socklen_t;
+int bind(int, const my_sockaddr*, my_socklen_t);
+
+using namespace std;
+
+int test01()
+{
+  int fd = 1;
+  my_sockaddr sa;           // N.B. non-const
+  size_t len = sizeof(sa);  // N.B. size_t not socklen_t
+  return bind(fd, &sa, sizeof(sa));
+}
+