Topic: Testing std::function objects for nullness


Author: Scott Meyers <smeyers@aristeia.com>
Date: Fri, 25 Sep 2009 18:29:39 CST
Raw View
Given a std::function object f, it's clear that I can say these things:

 if (f) ...
 if (!f) ...
 if (f == nullptr) ...
 if (f != nullptr) ...

>From what I can tell, however, I can't say this:

 if (f == 0) ...

Is that correct, or am I overlooking something that allows comparison
of a std::function object with the compile-time constant zero?

Thanks,

Scott

PS - The explicit implicit conversion to bool (I'm sorry, but what
else can you call "explicit operator bool()"?) is described as
"function capacity" in N2914.  Is this really a good name?  Capacity
means a very different thing in e.g., vector and string, and while I
understand that words can mean different things in different contexts,
is it really likely that people will say "what is the capacity of f?"
to inquire whether f is conceptually null?



--
[ 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: Nick Hounsome <nick.hounsome@googlemail.com>
Date: Sun, 27 Sep 2009 09:24:42 CST
Raw View
On 26 Sep, 01:29, Scott Meyers <smey...@aristeia.com> wrote:
> Given a std::function object f, it's clear that I can say these things:
>
>  if (f) ...
>  if (!f) ...
>  if (f == nullptr) ...
>  if (f != nullptr) ...
>
> >From what I can tell, however, I can't say this:
>
>  if (f == 0) ...
>

How can you have this for a template class without opening a can of
worms?

if( f == 42 )

if( f == 'x' )

> Is that correct, or am I overlooking something that allows comparison
> of a std::function object with the compile-time constant zero?
>
> Thanks,
>
> Scott
>
> PS - The explicit implicit conversion to bool (I'm sorry, but what
> else can you call "explicit operator bool()"?) is described as
> "function capacity" in N2914.  Is this really a good name?  Capacity
> means a very different thing in e.g., vector and string, and while I
> understand that words can mean different things in different contexts,
> is it really likely that people will say "what is the capacity of f?"
> to inquire whether f is conceptually null?

With a most generous interpretation I would expect the "capacity" to
be constant 1 and the "size" to be 1/true or 0/false
i.e. it could be null or not


--
[ 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 <smeyers@aristeia.com>
Date: Mon, 28 Sep 2009 03:35:24 CST
Raw View
Nick Hounsome wrote:
> How can you have this for a template class without opening a can of
> worms?
>
> if( f == 42 )
>
> if( f == 'x' )

By not permitting comparison of f with an int, only with a
compile-time expression evaluating to the constant zero, just like the
language permits for raw pointers.

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: "Thomas J. Gritzan" <phygon_antispam@gmx.de>
Date: Mon, 28 Sep 2009 12:35:47 CST
Raw View
Scott Meyers schrieb:
> Nick Hounsome wrote:
>> How can you have this for a template class without opening a can of
>> worms?
>>
>> if( f == 42 )
>>
>> if( f == 'x' )
>
> By not permitting comparison of f with an int, only with a
> compile-time expression evaluating to the constant zero, just like the
> language permits for raw pointers.

How to do that with library code?

--
Thomas

[ 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: Nick Hounsome <nick.hounsome@googlemail.com>
Date: Mon, 28 Sep 2009 12:36:16 CST
Raw View
On 28 Sep, 10:35, Scott Meyers <smey...@aristeia.com> wrote:
> Nick Hounsome wrote:
> > How can you have this for a template class without opening a can of
> > worms?
>
> > if( f == 42 )
>
> > if( f == 'x' )
>
> By not permitting comparison of f with an int, only with a
> compile-time expression evaluating to the constant zero, just like the
> language permits for raw pointers.
>
> Scott

But seems way more "magical" than for raw pointers.

Special treatment of basic types is one thing but magic template
classes just seems wrong.


--
[ 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: SG <s.gesemann@gmail.com>
Date: Mon, 28 Sep 2009 12:36:01 CST
Raw View
On 26 Sep., 02:29, Scott Meyers <smey...@aristeia.com> wrote:
> Given a std::function object f, it's clear that I can say these things:
>
>  if (f) ...
>  if (!f) ...
>  if (f == nullptr) ...
>  if (f != nullptr) ...
>
> >From what I can tell, however, I can't say this:
>
>  if (f == 0) ...
>
> Is that correct, or am I overlooking something that allows comparison
> of a std::function object with the compile-time constant zero?

N2914, 4.10/1 [conv.ptr]

 " ... A null pointer constant of integral type can be converted
   to an rvalue of type std::nullptr_t. ... "

Due to this I think that

  if (f == 0) {....}

is legal and invokes operator==(std::function<...> const&, nullptr_t);

Cheers,
SG


--
[ 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                      ]