BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Order of evaluation

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
José
Guest





PostPosted: Tue May 15, 2007 7:58 pm    Post subject: Order of evaluation Reply with quote



Dear friends

Is there a guaranteed order to execute an expression? And can part of
an expression be conditionally evaluated?

For example, is this safe:

int array[max];
if (index<max && array[index]==5)

Other example: will this function be called:

if (false && function())

Of course I can try and see what happens. But is there a guaranteed
answer?
--
José
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Tue May 15, 2007 8:52 pm    Post subject: Re: Order of evaluation Reply with quote



José <jose (AT) 127 (DOT) 0.0.1> writes:

Quote:
Is there a guaranteed order to execute an expression?

Usually. :)

Quote:
And can part of an expression be conditionally evaluated?

For built-in operators that have short-circuit evaulation logic, yes.
For example, operator|| will not evaluate the right argument if the
left one is true (since it already knows the overall result of the
expression ), and operator&& will not evaluate the right parameter if
the first expression evaluates to false, for similar reasons.

If you overload those operators for a user-defined type, then it will
behave like a normal function and evaluate both arguments before
invoking your function, and the order of evaulation of function
parameters is not guaranteed, and can change from platform to
platform, from compiler to compiler.)

Quote:
For example, is this safe:

int array[max];
if (index<max && array[index]==5)

Yes, that's safe.

Quote:
Other example: will this function be called:

if (false && function())

Function() will not be called. That is guaranteed.

--
Chris (TeamB);
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Tue May 15, 2007 8:59 pm    Post subject: Re: Order of evaluation Reply with quote



José <jose (AT) 127 (DOT) 0.0.1> writes:

Quote:
Is there a guaranteed order to execute an expression?

For some expressions. For others, there is no such guarantee.


Quote:
And can part of an expression be conditionally evaluated?

Yes.


Quote:
For example, is this safe:

int array[max];
if (index<max && array[index]==5)

It depends. The code doesn't check for negative values; so index could
be negative if its type is signed. Whether this makes the code unsafe
depends on the parts that you haven't shown us.


Quote:
Other example: will this function be called:

if (false && function())

No.

The built-in && guarantees that its right-hand sub-expression is only
evaluated if the left-hand has been evaluated to true.


Quote:
Of course I can try and see what happens. But is there a guaranteed
answer?

Yes, it's guaranteed.


FWIW, here is an example with unspecified behavior:

#include <iostream>
#include <ostream>

int f(int &i)
{
std::cout << i << ' ';
++i;
return 0;
}

int g(int &i)
{
std::cout << i << ' ';
i += 2;
return 0;
}

int main()
{
int i(0);
f(i)+g(i);
}

f(i) and g(i) will be evaluated both, but the order of their
evaluation is unspecified. You are likely to get different output from
different C++ implementations.
Back to top
Sergiy Kanilo
Guest





PostPosted: Tue May 15, 2007 10:39 pm    Post subject: Re: Order of evaluation Reply with quote

"Jose" <jose (AT) 127 (DOT) 0.0.1> wrote in message
news:7eij43pnao5iaom70ce3l57i9q1i8oe3mj (AT) 4ax (DOT) com...

Quote:
Is there a guaranteed order to execute an expression? And can part of
an expression be conditionally evaluated?

For example, is this safe:

int array[max];
if (index<max && array[index]==5)

No. The simpliest thing is that the index could be signed and negative,
and even if it in [0,max) range, the value array[index] is not initalized.
There is also possibility that index is of some user defined type, and
in this case anything is possible

Quote:
Other example: will this function be called:

if (false && function())

It could if the function returns some user defined type
for which the operator && is overloaded

Cheers,
Serge
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed May 16, 2007 12:04 am    Post subject: Re: Order of evaluation Reply with quote

"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote in message
news:86abw6rsnz.fsf (AT) explicit (DOT) atdesk.com...

Quote:
int array[max];
if (index<max && array[index]==5)

Yes, that's safe.

Provided that index is not < 0, that is.


Gambit
Back to top
José
Guest





PostPosted: Wed May 16, 2007 1:26 am    Post subject: Re: Order of evaluation Reply with quote

On Tue, 15 May 2007 12:04:11 -0700, "Remy Lebeau \(TeamB\)"
<no.spam (AT) no (DOT) spam.com> wrote in borland.public.cppbuilder.language.cpp:

Quote:
int array[max];
if (index<max && array[index]==5)

Yes, that's safe.

Provided that index is not < 0, that is.

Why yes, and the array must be initialised. It was a quick example to
demonstrate what I meant. I presume you understood that too.
--
José
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.