 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
José Guest
|
Posted: Tue May 15, 2007 7:58 pm Post subject: Order of evaluation |
|
|
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
|
Posted: Tue May 15, 2007 8:52 pm Post subject: Re: Order of evaluation |
|
|
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
|
Posted: Tue May 15, 2007 8:59 pm Post subject: Re: Order of evaluation |
|
|
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
|
Posted: Tue May 15, 2007 10:39 pm Post subject: Re: Order of evaluation |
|
|
"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
|
Posted: Wed May 16, 2007 12:04 am Post subject: Re: Order of evaluation |
|
|
"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
|
Posted: Wed May 16, 2007 1:26 am Post subject: Re: Order of evaluation |
|
|
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 |
|
 |
|
|
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
|
|