E
Edward Diener
For a ref object x, can I say "if (x)" rather than "if (x != nullptr)"
to test that x is pointing to something ?
to test that x is pointing to something ?
test that x is pointing to something ?
Do you say the answer is trivial? If so I must admit I disagree, but thenBruno van Dooren said: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).
Bruno van Dooren said: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.
I was more thinking of the lines of function or class template which worksof 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.
I'm afraid I don't quite understand. What do you mean with "native c++"
and "handle class"? Obviously, standard C++ doesn't have handles.
The language designers often claim that handles are very similar to
pointers.