Topic: Why =delete comparisons for std::function?


Author: Scott Meyers <usenet@aristeia.com>
Date: Sun, 18 Oct 2009 10:26:45 CST
Raw View
In TR1, a std::tr1::function object could be implicitly converted to an
unspecified null pointer type, so given two std::tr1::function objects f1 and
f2, the code

    if (f1 == f2) ...

might compile, because each object might be implicitly convertible to the same
unspecified null pointer type.  To prevent this, operator== and operator!= for
std:tr1::function objects was declared private and was left undefined.

During the transition from TR1 to C++0x, these functions were changed to be
"=delete":

    // 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;

However, the implicit conversion to bool for std::function was also made
explicit in C++0x, so given two std::function objects f1 and f2, the code

    if (f1 == f2) ...

won't compile, regardless of whether operator== and operator!= are deleted.

Is there some other possible hole in the type system that these function
declarations are plugging, or could they be removed from C++0x with no loss of
type safety?

Thanks,

Scott

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sun, 18 Oct 2009 15:35:08 CST
Raw View
On 18 Okt., 18:26, Scott Meyers <use...@aristeia.com> wrote:
> In TR1, a std::tr1::function object could be implicitly converted to an
> unspecified null pointer type, so given two std::tr1::function objects f1 and
> f2, the code
>
>     if (f1 == f2) ...
>
> might compile, because each object might be implicitly convertible to the same
> unspecified null pointer type.  To prevent this, operator== and operator!= for
> std:tr1::function objects was declared private and was left undefined.

Correct, this was necessary to support the so-called "safe-bool"
idiom.

> During the transition from TR1 to C++0x, these functions were changed to be
> "=delete":
>
>     // 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;
>
> However, the implicit conversion to bool for std::function was also made
> explicit in C++0x, so given two std::function objects f1 and f2, the code
>
>     if (f1 == f2) ...
>
> won't compile, regardless of whether operator== and operator!= are deleted.

Correct, see below.

> Is there some other possible hole in the type system that these function
> declarations are plugging, or could they be removed from C++0x with no loss of
> type safety?

The comment is now wrong and I'm convinced that the deleted comparison
functions of std::function could and should be removed. It's funny
that you
mention this now, because I'm just in the process of preparing a
library issue
that either suggests removing them or take the minimum action and
remove
the no longer valid comments about the "hole in the type system".

HTH & Greetings from Bremen,

Daniel Kr   gler



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Scott Meyers <usenet@aristeia.com>
Date: Mon, 19 Oct 2009 13:09:45 CST
Raw View
Daniel Kr   gler wrote:
The comment is now wrong and I'm convinced that the deleted comparison
functions of std::function could and should be removed. It's funny
that you
mention this now, because I'm just in the process of preparing a
library issue
that either suggests removing them or take the minimum action and
remove
the no longer valid comments about the "hole in the type system".

I strongly encourage you to advocate for removing them rather than
just removing the comment.  The functions are either necessary or they
are not, and eliminating the comment suggesting that they are
necessary when they're not won't make them any less unnecessary.

Scott


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]