473,414 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Parameter evaluation order on operator invocations

Hi all,

recently I came across a line of code like the following:

if seq.erase(seq.begin(), seq.end()) != seq.end()
/* ... */

It made me wonder if this is just bogus or if it even can invoke
undefined behaviour.

The answer depends, AFAICT, on the sequence of evaluation of both of
the parameters of operator!=.

1) I've read that the evaluation order of normal function parameters is
not defined.

2) OTOH, I seem to remember that at least some operators (&&, ||)
define a short-circuit logic (is this the right english term?), i.e.
the second parameter to operator&& is only ever evaluated if the first
one evaluates to true.

So the real question is: Are parameters of all operator calls
evaluated in their natural order (the same order in which the arguments
were declared), such that the code above just was bogus or is the order
only defined for certain special operators?
TIA,

aa

Oct 23 '06 #1
3 2783
andreas ames wrote:
recently I came across a line of code like the following:

if seq.erase(seq.begin(), seq.end()) != seq.end()
/* ... */
Parentheses seem to be missing around the expression...
It made me wonder if this is just bogus or if it even can invoke
undefined behaviour.
It can be. It can invoke UB. All depens on what 'seq' is.
The answer depends, AFAICT, on the sequence of evaluation of both of
the parameters of operator!=.
Why?
1) I've read that the evaluation order of normal function parameters
is not defined.
"Unspecified" is the preferred term.
2) OTOH, I seem to remember that at least some operators (&&, ||)
define a short-circuit logic (is this the right english term?), i.e.
the second parameter to operator&& is only ever evaluated if the first
one evaluates to true.
That's so only for built-in logical operators. BTW, that's why it is
not a good idea to overload those.
So the real question is: Are parameters of all operator calls
evaluated in their natural order (the same order in which the
arguments were declared), such that the code above just was bogus or
is the order only defined for certain special operators?
Overloaded operators are NO exception to the "evaluation order of
function argument is unspecified" rule. However, the code above is
not necessarily bogus *iff* erasure in 'seq' does _not_ invalidate
the 'end' iterator (since it's not the one being erased).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '06 #2
Victor Bazarov wrote:
andreas ames wrote:
if seq.erase(seq.begin(), seq.end()) != seq.end()
/* ... */

Parentheses seem to be missing around the expression...
You're right, thanks.
It made me wonder if this is just bogus or if it even can invoke
undefined behaviour.

It can be. It can invoke UB. All depens on what 'seq' is.
Oh, I missed that possibility (that there are sequences that don't
invalidate the past-the-end iterator, when contents are deleted).
Thanks for the hint.
The answer depends, AFAICT, on the sequence of evaluation of both of
the parameters of operator!=.

Why?
See above, I missed the possibility that the past-the-end iterator
might stay valid.
2) OTOH, I seem to remember that at least some operators (&&, ||)
define a short-circuit logic (is this the right english term?), i.e.
the second parameter to operator&& is only ever evaluated if the first
one evaluates to true.

That's so only for built-in logical operators. BTW, that's why it is
not a good idea to overload those.
Aha, I didn't know, that this doesn't hold for overloaded logic
operators.
Overloaded operators are NO exception to the "evaluation order of
function argument is unspecified" rule. However, the code above is
not necessarily bogus *iff* erasure in 'seq' does _not_ invalidate
the 'end' iterator (since it's not the one being erased).
I think it's still bogus (even for sequences with more durable
past-the-end operators) because I fail to see how the 'then' branch of
the if statement could ever be reached. Am I still missing sth.?
cheers,

aa

Oct 23 '06 #3
andreas ames wrote:
Victor Bazarov wrote:
>andreas ames wrote:
>>if seq.erase(seq.begin(), seq.end()) != seq.end()
/* ... */
[..]

I think it's still bogus (even for sequences with more durable
past-the-end operators) because I fail to see how the 'then' branch of
the if statement could ever be reached. Am I still missing sth.?
The bogusness of any code is in the eye of the beholder. The 'then'
branch may contain some code that when compiled achieves certain code
behaviour (instantiation of a template, for example), but does not
need to be executed. Without seeing the entire program how can we
label anything in it "bogus"? Take this example:

/* code fragment */
i =
/* end of fragment */

isn't it obvious how totally bogus it is?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: publictom | last post by:
Is the following statement true? "It is required that the operands to an operator be fully evaluated before the operator itself is evaluated." The only way I can think of that it would be...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
2
by: Jan Engelhardt | last post by:
Hi, I was told that order of evaluation is unspecified for functions, i.e. int f = 0; print_results(modify(&f), modify(&f), modify(&f)); where i.e. modify() increases f by one. In my case...
13
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.