Title
Deleted comparison functions of std::function not needed
Status
c++11
Section
[func.wrap.func]
Submitter
Daniel Krügler

Created on 2009-10-18.00:00:00 last changed 161 months ago

Messages

Date: 2010-11-23.13:22:14

Proposed resolution:

In [func.wrap.func]/1, class function change as indicated:

// 20.7.15.2.3, function capacity:
explicit operator bool() const;

// deleted overloads close possible hole in the type system
template<class R2, class... ArgTypes2>
  bool operator==(const function<R2(ArgTypes2...)>&) = delete;
template<class R2, class... ArgTypes2>
  bool operator!=(const function<R2(ArgTypes2...)>&) = delete;
Date: 2010-11-23.13:22:14

[ Adopted at 2010-11 Batavia ]

Date: 2010-10-21.19:06:53

[ Post-Rapperswil ]

Moved to Tentatively Ready after 5 positive votes on c++std-lib.

Date: 2009-10-18.00:00:00

The class template std::function contains the following member declarations:

// deleted overloads close possible hole in the type system
template<class R2, class... ArgTypes2>
  bool operator==(const function<R2(ArgTypes2...)>&) = delete;
template<class R2, class... ArgTypes2>
  bool operator!=(const function<R2(ArgTypes2...)>&) = delete;

The leading comment here is part of the history of std::function, which was introduced with N1402. During that time no explicit conversion functions existed, and the "safe-bool" idiom (based on pointers-to-member) was a popular technique. The only disadvantage of this idiom was that given two objects f1 and f2 of type std::function the expression

f1 == f2;

was well-formed, just because the built-in operator== for pointer to member was considered after a single user-defined conversion. To fix this, an overload set of undefined comparison functions was added, such that overload resolution would prefer those ending up in a linkage error. The new language facility of deleted functions provided a much better diagnostic mechanism to fix this issue.

The central point of this issue is, that with the replacement of the safe-bool idiom by explicit conversion to bool the original "hole in the type system" does no longer exist and therefore the comment is wrong and the superfluous function definitions should be removed as well. An explicit conversion function is considered in direct-initialization situations only, which indirectly contain the so-called "contextual conversion to bool" ([conv]/3). These conversions are not considered for == or != as defined by the core language.

History
Date User Action Args
2011-08-23 20:07:26adminsetstatus: wp -> c++11
2010-11-23 13:22:14adminsetmessages: + msg5399
2010-11-14 13:10:57adminsetstatus: voting -> wp
2010-11-08 14:14:39adminsetstatus: ready -> voting
2010-10-21 19:06:53adminsetmessages: + msg4764
2010-10-21 19:06:53adminsetstatus: new -> ready
2010-10-21 18:28:33adminsetmessages: + msg1278
2009-10-18 00:00:00admincreate