Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   can you do if((myptr) && (myptr->IsGreen()) without blowing up? (http://www.velocityreviews.com/forums/t621376-can-you-do-if-myptr-and-and-myptr-isgreen-without-blowing-up.html)

Angus 06-20-2008 04:20 PM

can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?

Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?

peter koch 06-20-2008 04:26 PM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
On 20 Jun., 18:20, Angus <anguscom...@gmail.com> wrote:
> If I need to check if a pointer is valid I usually do a if(ptr) check
> first.
>
> But I have seen code such as this:
>
> if((myptr) && (myptr->IsGreen())
>
> Is this valid?


Yes, this is guaranteed to work.
>
> Is it because evaluation is from left to right so that the if(ptr) bit
> is evaluated first. *Then only if pointer is valid is right function
> called?


This is what is required in the standard, and all compilers adhere to
this requirement that dates back to C.

I would avoid the pointers and clarify by writing: if (myptr != 0 &&
myptr->IsGreen()), but this is a personal choice.

/Peter

Kai-Uwe Bux 06-20-2008 04:27 PM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
Angus wrote:

> If I need to check if a pointer is valid I usually do a if(ptr) check
> first.
>
> But I have seen code such as this:
>
> if((myptr) && (myptr->IsGreen())
>
> Is this valid?


Yes.

> Is it because evaluation is from left to right so that the if(ptr) bit
> is evaluated first. Then only if pointer is valid is right function
> called?


The formal reason is the guarantee in [5.14/1]:

The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands are
true and false otherwise. Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is
false.


Best

Kai-Uwe Bux

Jim Langston 06-20-2008 05:04 PM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
"Kai-Uwe Bux" <jkherciueh@gmx.net> wrote in message
news:g3glp3$43d$1@aioe.org...
> Angus wrote:
>
>> If I need to check if a pointer is valid I usually do a if(ptr) check
>> first.
>>
>> But I have seen code such as this:
>>
>> if((myptr) && (myptr->IsGreen())
>>
>> Is this valid?

>
> Yes.
>
>> Is it because evaluation is from left to right so that the if(ptr) bit
>> is evaluated first. Then only if pointer is valid is right function
>> called?

>
> The formal reason is the guarantee in [5.14/1]:
>
> The && operator groups left-to-right. The operands are both implicitly
> converted to type bool (clause 4). The result is true if both operands
> are
> true and false otherwise. Unlike &, && guarantees left-to-right
> evaluation: the second operand is not evaluated if the first operand is
> false.


As a note, this is sometimes refered to as "short circuiting". It will also
work with ||
if ( cond1 || cond2 )

cond2 will only be evaluated if cond1 is false.



gw7rib@aol.com 06-20-2008 08:48 PM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
On 20 Jun, 18:04, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Kai-Uwe Bux" <jkherci...@gmx.net> wrote in message
>
> news:g3glp3$43d$1@aioe.org...
> > Angus wrote:

>
> >> If I need to check if a pointer is valid I usually do a if(ptr) check
> >> first.

>
> >> But I have seen code such as this:

>
> >> if((myptr) && (myptr->IsGreen())

>
> >> Is this valid?

>
> > Yes.

>
> >> Is it because evaluation is from left to right so that the if(ptr) bit
> >> is evaluated first. *Then only if pointer is valid is right function
> >> called?

>
> > The formal reason is the guarantee in [5.14/1]:

>
> > *The && operator groups left-to-right. The operands are both implicitly
> > *converted to type bool (clause 4). The result is true if both operands
> > are
> > *true and false otherwise. Unlike &, && guarantees left-to-right
> > *evaluation: the second operand is not evaluated if the first operand is
> > *false.

>
> As a note, this is sometimes refered to as "short circuiting". *It will also
> work with ||
> if ( cond1 || cond2 )
>
> cond2 will only be evaluated if cond1 is false.


Note however that this is a special feature of the operators && and
||. If you do something like:

x = a() + b();

there is no guarantee which of a and b will be called first.

red floyd 06-20-2008 09:47 PM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
gw7rib@aol.com wrote:
> On 20 Jun, 18:04, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> "Kai-Uwe Bux" <jkherci...@gmx.net> wrote in message
>>
>> news:g3glp3$43d$1@aioe.org...
>>> Angus wrote:
>>>> If I need to check if a pointer is valid I usually do a if(ptr) check
>>>> first.
>>>> But I have seen code such as this:
>>>> if((myptr) && (myptr->IsGreen())
>>>> Is this valid?
>>> Yes.
>>>> Is it because evaluation is from left to right so that the if(ptr) bit
>>>> is evaluated first. Then only if pointer is valid is right function
>>>> called?
>>> The formal reason is the guarantee in [5.14/1]:
>>> The && operator groups left-to-right. The operands are both implicitly
>>> converted to type bool (clause 4). The result is true if both operands
>>> are
>>> true and false otherwise. Unlike &, && guarantees left-to-right
>>> evaluation: the second operand is not evaluated if the first operand is
>>> false.

>> As a note, this is sometimes refered to as "short circuiting". It will also
>> work with ||
>> if ( cond1 || cond2 )
>>
>> cond2 will only be evaluated if cond1 is false.

>
> Note however that this is a special feature of the operators && and
> ||. If you do something like:
>
> x = a() + b();
>
> there is no guarantee which of a and b will be called first.


Also, if operator&&() or operator||() is overloaded, there is also no
such guarantee.

Noah Roberts 06-20-2008 10:08 PM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
red floyd wrote:

> Also, if operator&&() or operator||() is overloaded, there is also no
> such guarantee.


Only criminals and madmen implement them otherwise.

Greg Herlihy 06-21-2008 02:58 AM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
On Jun 20, 3:08*pm, Noah Roberts <u...@example.net> wrote:
> red floyd wrote:
> > Also, if operator&&() or operator||() is overloaded, there is also no
> > such guarantee.

>
> Only criminals and madmen implement them otherwise.


But there is no way to specify the order in which the operands will be
evaluated when implementing an overloaded operator&&(), operator&&()
or (for that matter) operator,(). For this reason, overloading any one
of these three operators is seldom a good idea.

Greg

Greg Herlihy 06-21-2008 03:00 AM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
On Jun 20, 3:08 pm, Noah Roberts <u...@example.net> wrote:
> red floyd wrote:
> > Also, if operator&&() or operator||() is overloaded, there is also no
> > such guarantee.

> Only criminals and madmen implement them otherwise.


You must mean that only criminals and madmen implement these overloads
-at all-.

Because there is no way for anyone to specify the order in which the
operands will be
evaluated when implementing an overloaded operator&&(), operator||()
or (for that matter) operator,(). For this reason, overloading any
one
of these three operators is seldom a good idea.

Greg


Juha Nieminen 06-21-2008 08:58 AM

Re: can you do if((myptr) && (myptr->IsGreen()) without blowing up?
 
peter koch wrote:
>> if((myptr) && (myptr->IsGreen())
>>
>> Is this valid?

>
> Yes, this is guaranteed to work.


Nitpicking, but it's guaranteed to work only if myptr actually points
to an object of the proper type. If it points to garbage (which could be
achieved, for example, through reinterpret_cast), obviously UB will
happen. But yeah, this is just nitpicking.


All times are GMT. The time now is 07:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57