This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++11 status.

1240. Deleted comparison functions of std::function not needed

Section: 22.10.17.3 [func.wrap.func] Status: C++11 Submitter: Daniel Krügler Opened: 2009-10-18 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [func.wrap.func].

View all issues with C++11 status.

Discussion:

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" (7.3 [conv]/3). These conversions are not considered for == or != as defined by the core language.

[ Post-Rapperswil ]

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

[ Adopted at 2010-11 Batavia ]

Proposed resolution:

In 22.10.17.3 [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;