473,802 Members | 1,940 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

"Jerry Coffin" <jc*****@taeus. com> wrote in message news:MP******** *************** *@news.clspco.a delphia.net...
It's not required because nothing in the
standard requires it.


Actually, the omission of a defined behavior in the standard is UNDEFINED
BEHAVIOR. The ordering of operations is specifically stated as being arbitrary.
Nov 14 '05 #21
In article <40************ ***********@new s.newshosting.c om>,
ro*@sensor.com says...

"Jerry Coffin" <jc*****@taeus. com> wrote in message news:MP******** *************** *@news.clspco.a delphia.net...
It's not required because nothing in the
standard requires it.


Actually, the omission of a defined behavior in the standard is UNDEFINED
BEHAVIOR. The ordering of operations is specifically stated as being arbitrary.


Perhaps you should have actually read what I said -- I said that the
order of evaluation of subexpressions is unspecified. If you want that
more exactly, the C standard says (6.5/3):

Except as specified later (for the function call (), &&, ||, ?:
and comma operators), the order of evaluation of subexpressions
and the order in which side effect take place are both
unspecified.

The bit you've quoted above was referring specifically to whether there
was a requirement for an implementation to issue a diagnostic when the
output depends upon unspecified behavior. As I said, the answer is no,
and the reason is because no part of the standard requires a diagnostic.

The omission of behavior leading to undefined behavior ONLY relates to
the behavior the program at run time, NOT to behavior during
translation. The requirement for diagnostics is (5.1.1.3/1):

A conforming implementation shall produce at least one diagnostic
message (identified in an implementation defined manner) if a
preprocessing translation unit or translation unit contains a
violation of any syntax rule or constraint, even if the behavior
is also explicitly specified as undefined or implementation-
defined. Diagnostic messages need not be produced in other
circumstances.
Therefore, (as I said before) a diagnostic is not required because
nothing says it is -- i.e. there is no constraint (and clearly no syntax
rule) that says the output of a program cannot depend upon unspecified
behavior. Therefore, this is one of those "other circumstances" in
which a diagnostic need not be produced.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 14 '05 #22
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote in message news:<bu******* ***@chessie.cir r.com>...
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 expression does require a diagnostic. ;)

--
Peter
Nov 14 '05 #23
> 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;

?


Error: invalid lvalue in increment
Nov 14 '05 #24
On Mon, 19 Jan 2004 20:21:24 GMT in comp.lang.c++, Jerry Coffin
<jc*****@taeus. com> was alleged to have written:
Therefore, (as I said before) a diagnostic is not required because
nothing says it is


That's exactly correct, but not terribly informative. I'm think the
poster asking "why not" would like to know why the standard doesn't
require it. http://www.netfunny.com/rhf/jokes/98/Aug/sales.html

For one reason, because the standard generally requires a diagnostic
only for programs that are ill-formed, and this isn't.

For another, because it's unreasonable to expect the compiler to be able
to figure it out. Should a nuisance warning be produced for
int i = f() + g();
Nov 14 '05 #25
Peter Nilsson <ai***@acay.com .au> spoke thus:
j=++i+++i;
That expression does require a diagnostic. ;)


foo.c:4: error: you are dumb

;)

--
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 #26
Christopher Benson-Manica wrote:
Peter Nilsson <ai***@acay.com .au> spoke thus:

j=++i+++i;


That expression does require a diagnostic. ;)

foo.c:4: error: you are dumb

;)


foo.c:4: error: you lied when you told me this was a program

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.
Nov 14 '05 #27

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
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
0
10304
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
1
7598
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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.