473,802 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order of evaluation and better declaration of functions

This post contains one question and one proposal.

A. May I know why order of evaluation of arguments is not specified in
C/C++?

I asked a question in comp.lang.c++ for the following possibility and
because the languages do not specify the order of evaluation, doing so
was an error.

int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
....
}

(Folks there suggested either to use polymorphism or to call
constructor of another class.)

B. If such an order is specified, we can not only default an argument
based on previous arguments, we can declare methods / functions
better. Think of this:
// file foo.h
// This example gains from fixed order of evaluation of arguments
// but it is not necessary to have fixed order if we disallow
references to
// previous arguments from an argument

(int *) B::f (
int i { //document here and assert at run time
i > 0 && i < INT_MAX
},
int j = i + 1 {
j > i || j < 0
},
int *k {
k != NULL && k!= some_global
}
) // end of argument documented assertion now return value's
turn
(
!= NULL
);

Upon execution, prologue to B::f may take each argument and assert.
The first argument can be asserted only against constants and globals.
If order of evaluation is not specified, other arguments follow the
suite
else their assertions and default assignment if any, may include
previous argument.
Then B::f's body gets executed
Then B::f's return value (if any) is checked against assertions if
any.

[I feel shy of suggesting a "default return value" for a function.]

This scheme has three advantages:
1. It facilitates communicating pre-conditions, post-conditions very
clearly (BIG plus)
2. It makes definition of function f clearer
3. Splint and other lints depend on specially annotated code to write
assumptions about the code. This approach is replacing annotation with
assertion and making code more robust.

I can see five problems with this scheme - some of them are easy to
solve
1. It may not always be possible to resolve globals and externs in a
..h file.
2. It may come in way of "counting the clock cycles" styled C because
of hidden assertions. [ A good code must have those assertions anyway.
#3 below solves this.]
3. These assertions must be "bypass-able" for released code. [However,
provision of an option in compiler would do that. Even then, all
argument related assertions are now viewable at a glance in a .h
file.]
4. It makes necessary to link the code to <assert.h> or equivalent.
[Once again, a good code must have this anyway.]
5. Function calls within assertions may become nasty. [At the worst,
we can disallow function calls there.]

Your comments?
-Bhushit
Nov 14 '05
26 2513
Risto Lankinen wrote:
"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message
news:bt******** **@hercules.bti nternet.com...
Bhushit Joshipura wrote:

A. May I know why order of evaluation of arguments is not specified in
C/C++?
There is no particular benefit to
pinning down the order of evaluation of arguments, and not doing so gives
compiler writers licence to optimise evaluation orders for their


platforms.

I politely disagree. Constructs in the gray area of C++
that anyway work consistently within one compiler have
a tendency of tempting the beginning programmers into
using them thru trial-and-error. The cost of porting such
code to another compiler could be reduced significantly
if at least the most common "undefined behaviour" were
instead defined in the standard.


True, but being newbie-proof isn't, IMO, in the "philosophy " of C++.

It seems to me that the best way forward is for the compiler to emit
warnings for code that depends on the order of evaluation.

The usual counter-argument against defining the behaviour
of what still is labeled as undefined behaviour has outlived
its original intention of efficiency.
The second original counter-argument is that there's conflicting
implementations , and that still applies.
The translation of source
language expressions is no longer one-to-one mapped with
machine instructions anyway, mostly due to the emergence
of efficient optimization techniques. The language design is
oriented towards abstraction everywhere else,
Huh? Are we talking about the same C++ here?
so why do
we still need to stick with (largely miniscule or imaginary)
machine dependent efficiency argument when it comes to
expression evaluation?
Well, we don't; there are lots of languages that do specify the order of
expression evaluation in function calls. But it seems to me that there
is a real need for a language that puts performance first, and that
means enabling microoptimizati ons in its design. I'm not saying that C
or C++ are ideal languages in that sense, but they're pretty much the
best we've got, and there are a lot of languages where performance is a
secondary concern.

Another reason to avoid specifying this sort of thing is future
proofing. It's possible that this particular optimization will become
more valuable in the future (e.g. parallel machines might evaluate the
arguments in a different order in each thread to avoid competing for
shared resources). It's a mistake to pointlessly limit future
implementations .

Cheers!

- Risto -


-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #11
In comp.lang.c Peter Ammon <ge******@splin termac.com> wrote:
True, but being newbie-proof isn't, IMO, in the "philosophy " of C++.
Since comp.lang.c is still (gratuitously?) on the crosspost list, I'll
just note that the same could likely be said about C. Java is more
friendly than either C or C++, but users of those languages might
argue that that friendliness comes at the expense of flexibility.
It seems to me that the best way forward is for the compiler to emit
warnings for code that depends on the order of evaluation.


That might be nice. To clc: Is a diagnostic required in such
circumstances? If not, why not? (replies to this question should
abandon the crosspost).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #12
Christopher Benson-Manica wrote:

In comp.lang.c Peter Ammon <ge******@splin termac.com> wrote:
True, but being newbie-proof isn't, IMO, in the "philosophy " of C++.


Since comp.lang.c is still (gratuitously?) on the crosspost list, I'll
just note that the same could likely be said about C. Java is more
friendly than either C or C++, but users of those languages might
argue that that friendliness comes at the expense of flexibility.
It seems to me that the best way forward is for the compiler to emit
warnings for code that depends on the order of evaluation.


That might be nice. To clc: Is a diagnostic required in such
circumstances? If not, why not? (replies to this question should
abandon the crosspost).


If you were writing a compiler, how would you detect the situation ?
Seems like a lot of trouble to detect
code that depends on the order of evaluation.
I have have no idea what constitutes all the different kinds of code
which depend on the order of evaluation.

/* BEGIN new.c */

#include <stdio.h>

int count(void)
{
static counter;

return counter++;
}

int main(void)
{
printf("%d %d\n", count(), count());
return 0 ;
}

/* END new.c */

--
pete
Nov 14 '05 #13
pete <pf*****@mindsp ring.com> spoke thus:
int count(void)
{
static counter; return counter++;
} int main(void)
{
printf("%d %d\n", count(), count());
return 0 ;
}


Oh, I see. Right. Well, even if a compiler can't find *every*
instance of OOE code, mightn't it still be nice to require a
diagnostic for

j=++i+++i;

?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #14
Christopher Benson-Manica wrote:

pete <pf*****@mindsp ring.com> spoke thus:
int count(void)
{
static counter;

return counter++;
}

int main(void)
{
printf("%d %d\n", count(), count());
return 0 ;
}


Oh, I see. Right. Well, even if a compiler can't find *every*
instance of OOE code, mightn't it still be nice to require a
diagnostic for

j=++i+++i;

?


That's an example of undefined behavior.

I thought you you were talking about unspecified behavior
resulting from the unspecified order of evaluation
of function arguments.

--
pete
Nov 14 '05 #15
pete <pf*****@mindsp ring.com> spoke thus:
I thought you you were talking about unspecified behavior
resulting from the unspecified order of evaluation
of function arguments.


Um, perhaps. My apologies.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #16
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Oh, I see. Right. Well, even if a compiler can't find *every*
instance of OOE code, mightn't it still be nice to require a
diagnostic for

j=++i+++i;


Problem: how do you propose we formulate the rule deciding what to
demand a diagnostic for, and what to leave undefined, without either
only catching all the trivial, unrealistic cases like your example, or
being so complicated that it's overly hard to implement?

Richard
Nov 14 '05 #17
Christopher Benson-Manica wrote:
Peter Ammon <ge******@splin termac.com> wrote:
.... snip ...
It seems to me that the best way forward is for the compiler to
emit warnings for code that depends on the order of evaluation.


That might be nice. To clc: Is a diagnostic required in such
circumstances? If not, why not? (replies to this question
should abandon the crosspost).


That would be pleasant. First, however, please describe how you
decide that proposition for:

foobar(foo(*glo rp) - bar(*glorp));

when foo, bar, foobar are in other source files, although you have
proper prototypes. Omit no detail of the decision process. Note
that the other files may not even have been written yet.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #18
CBFalconer <cb********@yah oo.com> spoke thus:
That would be pleasant. First, however, please describe how you
decide that proposition for: foobar(foo(*glo rp) - bar(*glorp)); when foo, bar, foobar are in other source files, although you have
proper prototypes. Omit no detail of the decision process. Note
that the other files may not even have been written yet.


See my other reply where I admit that my original suggestion was
fatally flawed for precisely this reason. Of course, divine
intervention is always an option, but the Standard doesn't specify
which deity should be invoked ;)

(seriously, apparently I'm quite clue-impaired today - sorry and
thanks!)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #19
In article <bu**********@c hessie.cirr.com >, at***@nospam.cy berspace.org
says...

[ ... ]
It seems to me that the best way forward is for the compiler to emit
warnings for code that depends on the order of evaluation.
That might be nice. To clc: Is a diagnostic required in such
circumstances? If not, why not?


No -- order of evaluation is unspecified behavior. If the output
depends on unspecified behavior, the code is not strictly conforming,
but no diagnostic is required. It's not required because nothing in the
standard requires it.
(replies to this question should abandon the crosspost).


I've trimmed the cross-post to c.l.c and c.l.c++. C++ doesn't define
"strictly conforming", but otherwise it applies to both. Even without a
specific phrase to denote it, many C++ programmers are quite interested
in writing code that is maximally portable, so the concept seems (to me)
to remain relevant.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 14 '05 #20

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

Similar topics

16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10536
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.