Re: Testing for nullptr for a ref object

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



For a ref object x, can I say "if (x)" rather than "if (x != nullptr)"
to test that x is pointing to something ?

Yes, you can.
a quick test would have given you the answer immediatly.

Do you say the answer is trivial? If so I must admit I disagree, but then
I'm must admit I haven't read much of the C++/CLI standard.

Anyway, my interpretation is that the compiler will consider a conversion
from x to bool better than the check against nullptr.

So if a conversion from x to bool exists it will be chosen over the
!= nullptr check (BTW: there is no standard conversion from
handle-to-bool as there is for native pointers).

Hi Holger,
Your example also works with native C++. if you create a handle class that
has a bool conversion, you can
do something like

if(myHandle)
{
}
to check if the handle in your object is valid or not.

there was a discussion about this topic 1 or 2 weeks ago.
my preference is to only use 'if' with real bools that are either return
values or the result of an equation.

my point was that if you want to know for sure, just do a quick test.

of course it could be dangerous to rely on the automatic handle to bool
conversion
because if soemone adds a conversion to bool later in the development
traject, your code suddenly starts doing funny things.
I admit I didn't think of the case where there was a conversion to bool.

Thanks for correcting me.

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@xxxxxxxxxxx
Remove only "_nos_pam"



.



Relevant Pages

  • Re: I Need an IsNumeric Method
    ... public bool IsNumeric ... //Try a double conversion. ... try to convert and catch the exception ... > o Don't forget that .5 is generally considered okay, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: boolean usage
    ... "An object declared as type _Bool is large enough to store the ... That mearely constrains the values that result from conversion. ... The width includes any padding bits. ... "The precision of an integer type is the number of bits it uses ...
    (comp.lang.c)
  • Re: Testing for nullptr for a ref object
    ... my interpretation is that the compiler will consider a conversion ... from x to bool better than the check against nullptr. ... So if a conversion from x to bool exists it will be chosen over the ... X^ x = gcnew X; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: boolean usage
    ... Peter Nilsson writes: ... "An object declared as type _Bool is large enough to store the ... That mearely constrains the values that result from conversion. ... 6.5.4p4 "A cast that specifies no conversion has no effect ...
    (comp.lang.c)
  • Re: void * instead of bool
    ... Earth would I definitily not want that to compile? ... One is that perhaps the original design dates from a time before 'bool' ... ifstream object to a constructor where one overload takes a bool. ... the latter case the conversion to ...
    (comp.lang.cpp)