473,385 Members | 1,536 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,385 software developers and data experts.

Ambiguous Expression

Colleagues,

Suppose I have (in simplified form, of course)

struct Vect2D {
double x, y;
Vect2D(double x_, double y_) : x(x_), y(y_) {}
};

which I have to construct with random numbers:

Vect2D v(rand(), rand());

But this is "Ambiguous Expression" - i.e. "...language does not
guarantee the order in which arguments to a function call are
evaluated."

I was beaten by such type of code indeed - release and debug builds
behave differently :(

Above code is so typical, do I have to force explicit order of argument
evaluation? It would not be so compact and nice.
--
Vladimir

Jul 22 '05 #1
16 1651
Vladimir wrote:
Colleagues,

Suppose I have (in simplified form, of course)

struct Vect2D {
double x, y;
Vect2D(double x_, double y_) : x(x_), y(y_) {}
};

which I have to construct with random numbers:

Vect2D v(rand(), rand());

But this is "Ambiguous Expression" - i.e. "...language does not
guarantee the order in which arguments to a function call are
evaluated."

I was beaten by such type of code indeed - release and debug builds
behave differently :(

Above code is so typical, do I have to force explicit order of argument
evaluation? It would not be so compact and nice.


Of course you do. You always have to account for side effects of function
calls. However, let me note here that one shouldn't really care about the
order when _random_ values are concerned, should one?

V
Jul 22 '05 #2
Victor Bazarov wrote:
However, let me note here that one shouldn't really care about the
order when _random_ values are concerned, should one?
I thought so too :)
But the actual problem isn't the order itself. Modern compilers with
global opimizations and inlining will reach a situation like

call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)

Vect2D v(rand(), rand());


Of course you do. You always have to account for side effects of

function calls.


But C++ is quite powerful in allowing to use stuff on the fly which
really helps in large programs. What can be most compact, preferably
one-line solution to this problem then?
--
Vladimir

Jul 22 '05 #3
"Vladimir" <vl*********@yahoo.com> wrote...
Victor Bazarov wrote:
However, let me note here that one shouldn't really care about the
order when _random_ values are concerned, should one?


I thought so too :)
But the actual problem isn't the order itself. Modern compilers with
global opimizations and inlining will reach a situation like

call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)


The expression above has undefined behaviour because the object 'i' has
its _stored_value_ changed more than once between sequence points.
> Vect2D v(rand(), rand());


Of course you do. You always have to account for side effects of

function
calls.


But C++ is quite powerful in allowing to use stuff on the fly which
really helps in large programs. What can be most compact, preferably
one-line solution to this problem then?


There isn't any. Use separately declared/defined/initialised objects:

int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);

Victor
Jul 22 '05 #4
Victor Bazarov wrote:
There isn't any. Use separately declared/defined/initialised objects:

int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);

What about this?
int x;

Vect2D v((x=rand(), rand()), x);
:-)


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Ioannis Vranos wrote:

int x;

Vect2D v((x=rand(), rand()), x);

That's worse. The second argument to the
v initializer may be evaluated before the first.
Jul 22 '05 #6

Victor Bazarov wrote:
call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)
The expression above has undefined behaviour because the object 'i'

has its _stored_value_ changed more than once between sequence points.
Similar situation is with rand() - after inlining, internal value
(used to hold random state) is changed more than once - that's why
the problem occurs.

There isn't any. Use separately declared/defined/initialised objects:
int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);


I wish we could always remember to use this when needed.
What about encapsulating such functions with internal state
into some special classes which would prevent problems
(possibly by preventing inlining, etc.)?
This would make coding much more reliable - which is
essential in large serious projects.

P.S. These little things are really important, people.
They usually make the difference between 99% and 100%
bug-free software, so I think we shouldn't ignore them.
--
Vladimir

Jul 22 '05 #7
Vladimir wrote:
Victor Bazarov wrote:
call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)


The expression above has undefined behaviour because the object 'i'


has
its _stored_value_ changed more than once between sequence points.

Similar situation is with rand() - after inlining, internal value
(used to hold random state) is changed more than once - that's why
the problem occurs.


No, because there's a sequence point before each call to rand.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #8
Vladimir wrote:
Victor Bazarov wrote:
call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)
The expression above has undefined behaviour because the object 'i'


has
its _stored_value_ changed more than once between sequence points.

Similar situation is with rand() - after inlining, internal value
(used to hold random state) is changed more than once - that's why
the problem occurs.


Similar, but no undefined behaviour, only unspecified order of calls.
Every function call is surrounded by sequence points, so even with
inlining there would be at least four of them between the program
decided to call the first 'rand' and calling the 'call' function. So,
the change to some stored value (the side effect of 'rand') does not
happen more than once between sequence points.
There isn't any. Use separately declared/defined/initialised


objects:
int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);

I wish we could always remember to use this when needed.


And I wish I were young, slim, and healthy.
What about encapsulating such functions with internal state
into some special classes which would prevent problems
(possibly by preventing inlining, etc.)?
You can try limiting the members of your programming team to using
some kind of class for that, or a macro, or whatever would resolve
this issue, but the language does not provide a mechanism (yet) to
catch all instances of unspecified behaviour. Of course we can always
hope for better tools at our disposal...
This would make coding much more reliable - which is
essential in large serious projects.
I believe you could use some kind of "PC-lint"-like code checker that
might catch that.
P.S. These little things are really important, people.
They usually make the difference between 99% and 100%
bug-free software, so I think we shouldn't ignore them.


Of course we shouldn't. And _we_ won't. It's the programmers who don't
read comp.lang.c++ we should be worrying about :-)

V
Jul 22 '05 #9

Victor Bazarov wrote:
So, the change to some stored value (the side effect of 'rand')
does not happen more than once between sequence points.


Here's smallest code reproducing the problem, where func() represents
typical rand() implementation in very simplified form:

inline int func()
{
static int state = 0;
return ++state;
}

int main()
{
std::cout << func() << func() << std::endl;
return 0;
}

Debug build produces "21" which is ok, but Release build
outputs "22" which ruins expected sequence-generating behavior.
I tested it with vc++ 6.0 and I'm interested what other compilers
would offer (note: global optimizations were heavily used).
--
Vladimir

Jul 22 '05 #10
Vladimir wrote:
Victor Bazarov wrote:
So, the change to some stored value (the side effect of 'rand')
does not happen more than once between sequence points.

Here's smallest code reproducing the problem, where func() represents
typical rand() implementation in very simplified form:

inline int func()
{
static int state = 0;
return ++state;
}


To make it compile on all compilers, add:

#include <iostream> // for 'std::cout'
#include <ostream> // for 'std::endl'

int main()
{
std::cout << func() << func() << std::endl;
return 0;
}

Debug build produces "21" which is ok, but Release build
outputs "22" which ruins expected sequence-generating behavior.
I tested it with vc++ 6.0 and I'm interested what other compilers
would offer (note: global optimizations were heavily used).


VC++ v7.1 produces "21" in debug mode and "22" in release mode (no
surprises there).

VC++ v8.0 Beta produces "21" in both debug and release modes.

Which is not to say that any of them are "correct", only that they
differ, as they may.

Victor
Jul 22 '05 #11
Victor Bazarov wrote:
=> VC++ v7.1 produces "21" in debug mode and "22" in release mode (no surprises there).

VC++ v8.0 Beta produces "21" in both debug and release modes.

Which is not to say that any of them are "correct", only that they
differ, as they may.


That is, "21" or "12" is okay, but "22" is definitely wrong, because it
violates the rules about sequence points.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #12
Pete Becker wrote:
Victor Bazarov wrote:

=> VC++ v7.1 produces "21" in debug mode and "22" in release mode (no
surprises there).

VC++ v8.0 Beta produces "21" in both debug and release modes.

Which is not to say that any of them are "correct", only that they
differ, as they may.


That is, "21" or "12" is okay, but "22" is definitely wrong, because it
violates the rules about sequence points.


Ah... Good point (no pun intended). So, optimization should not prevent
any side effects from taking place, yes? My guess is that circumventing
side effects is only allowed in particular cases and they are described in
the Standard explicitly.

V
Jul 22 '05 #13
Victor Bazarov wrote:
Ah... Good point (no pun intended). So, optimization should not prevent
any side effects from taking place, yes? My guess is that circumventing
side effects is only allowed in particular cases and they are described in
the Standard explicitly.


They're not described explicitly, but the "as if" rule (1.5/1) is the
thing to look to. The standard specifies the observable behavior of
well-formed programs. The behavior of this program depends on
unspecified behavior, but that doesn't make it ill-formed, so the
compiler must produce a program with the observable behavior specified
by the standard. It can't blow away the sequence point after the first
call (in the generated code, not the fist in the source code) to func.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #14
So,

vc++ v6.0 debug "21", release "22"
vc++ v7.1 debug "21", release "22"
vc++ v8.0 Beta "21" in both debug and release modes.

Something tells me v8.0 *Final* will catch up with previous versions :)
ok, it's OT - I'm hiding right now.

--
Vladimir

Jul 22 '05 #15

Pete Becker wrote:
by the standard. It can't blow away the sequence point after the first call (in the generated code, not the fist in the source code) to

func.
IMHO there is no sequence point between arg1 and arg2 evaulations in a
call(arg1, arg2);

is it?
--
Vladimir

Jul 22 '05 #16
Vladimir wrote:
Pete Becker wrote:
by the standard. It can't blow away the sequence point after the


first
call (in the generated code, not the fist in the source code) to


func.
IMHO there is no sequence point between arg1 and arg2 evaulations in a
call(arg1, arg2);

is it?


It depends on what 'arg1' and 'arg2' are. If they are function calls,
there is _always_ a sequence point between them (before the second call,
which is not necessarily to evaluate 'arg2'). That's what Pete said.

And it has nothing to do with opinions. It's specified by the Standard.

V
Jul 22 '05 #17

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

Similar topics

2
by: ALAN MEWS | last post by:
Hi, Hoping someone can help me. I'm fairly proficient in using MS Access 2000, but got the following problem and would very grateful for some advice as I am setting up (trying) this database for a...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
3
by: Arpan | last post by:
The following code exists in a class file named "Users.vb": Namespace Users Public Class UserDetails Public FirstName As String Public LastName As String Public UserName As String Public...
2
by: Tim H | last post by:
Why is this ambiguous: ------------------------------------------------ #include <boost/shared_ptr.hpp> class base {}; class derived: public base {}; class other {}; void...
5
by: Zeppe | last post by:
Dear all, I have the following problem, that I'll try to explain with a very minimal example: class A { }; class B
12
by: Nathan Sokalski | last post by:
I have several CustomControls that I have written for my project. However, when I try to compile I recieve the following warning & errors: Warning 32 Could not resolve this reference. Could not...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.