"Roel Schroeven" <j4*******@sneakemail.com> wrote in message
news:kx***************@afrodite.telenet-ops.be...
Ivan Vecerina wrote: or my personally preferred style:
while( !! listfile.getline(buffer, 100) )
What's the difference between that and
while (listfile.getline(buffer, 100))
In this case, there is no actual difference.
But the latter line relies on an *implicit* conversion
to bool. Actually, if you look into it, this is not just
a conversion to bool, but a conversion to void* -- that's
because there are some tricky issues related to such an
implicit conversion operator, so the standard library
provides the latter conversion to mimic a bool() operator.
Even as it is, one could still get in trouble:
void Verify(bool testThatMustBeTrue);
void Verify(void* pointerThatMustBeValid);
If you were to write:
Verify( listfile.getline(buffer,100) );
It is the second overload of Verify that would be called.
(and the non-zero pointer that is returned by the void*
conversion could be some invalid or illegal address ).
These implicit conversion issues do not apply when
using the operator !() overloaded by the stream classes
(which returns a bool, but with the opposite logical value).
Now, as I said, using the " !! " operator is a
style convention. I have adopted it since I've seen
it used a couple years ago, and never needed to go back.
(though, as for other style issues, I remain flexible).
Because implicit conversions to bool have some caveats,
you will see that several coding standards require
the use of:
if( myPtr != NULL ) and if( myPtr == NULL )
instead of:
if( myPtr ) and if( ! myPtr )
To avoid a common mistake: if( myPtr = NULL )
some even advise the following style:
if( NULL != myPtr ) and if( NULL == myPtr )
Instead of that, I just like the symmetry
and simplicity of writing:
if( !! myPtr ) and if( ! myPtr )
In all circumstances I know, " !! " can be considered
and used as a built-in "convert-to-bool" operator.
It also works nicely with stream-classes and the like
that provide a conversion to bool to check validity.
And in our own classes, when a similar validity test
is needed, we only provide an operator!(), and do not
mess with implicit conversion operators.
So it is just a style convention we adopted,
and we think it has real benefits compared to
other approaches.
Best regards,
--
Ivan Vecerina, Dr. med. <>
http://www.post1.com/~ivec
Soft Dev Manager, XiTact <>
http://www.xitact.com
Brainbench MVP for C++ <>
http://www.brainbench.com