Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Unpredictab...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 12 Topic 45818 of 47034
Post > Topic >>

Re: Unpredictable nature of increment operators

by Jerry Coffin <jcoffin@[EMAIL PROTECTED] > May 10, 2008 at 08:47 AM

In article <g0216m$pqc$1@[EMAIL PROTECTED]
>, v.Abazarov@[EMAIL PROTECTED]
 
says...

[ ... ]

> For example, it's not undefined behaviour to do
> 
>     f(i++) + ++i;
> 
> IIRC, but the results are unspecified. 

I believe this still gives undefined behavior. There are three sequence 
points in this expression: before f is called, when f returns, and at 
the end of the expression. The compiler can generate code that evaluates 
the pre-increment, then the post-increment, then the function call. 
There's no sequence point between the pre- and post-increment, so the 
result is undefined behavior.

> And if your class has the
> operators ++() or ++(int) overloaded, the results are even well-
> defined and predictable.  For 'int' they aren't...

Well-defined but not necessarily predictable. The fact that it's a 
function call imposes a sequence point as the function is called, and as 
it returns -- but still doesn't require that the functions be called in 
any particular order, so the value as the function is entered can be 
unpredictable. For example:

#include <iostream>

class X { 
	int value;
public:
	X() : value(0) {}
	X(int v) : value(v) {}
	X &operator++() { 
		++value; 
		return *this; 
	}
	X operator+(X const &other) { 
		return X(value + other.value);
	}
	friend std::ostream &operator<<(std::ostream &os, X const &x) { 
		return os << x.value;
	}
};

int main() { 
	X x, left, right;
	std::cout << (left= ++x) + (right = ++x) << "\n";
	std::cout << "Left = " << left << "\n";
	std::cout << "Right = " << right << "\n";
	return 0;
}

Now, this gives defined behavior -- regardless of the order of 
evaluation, there is always a sequence point between executing the two 
increment operators. Nonetheless, the compiler is free to produce code 
that evaluates either the left or the right sub-expression first, so the 
values of 'left' and 'right' aren't entirely predictable.

For example, using Comeau, I get Left = 1 and Right = 2. Using G++ or 
Microsoft, I get Left = 2 and Right = 1.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
 




 12 Posts in Topic:
Unpredictable nature of increment operators
bintom <binoythomas110  2008-05-08 17:16:56 
Re: Unpredictable nature of increment operators
Ian Collins <ian-news@  2008-05-09 12:17:58 
Re: Unpredictable nature of increment operators
Martin York <Martin.Yo  2008-05-09 10:11:08 
Re: Unpredictable nature of increment operators
"Victor Bazarov"  2008-05-09 13:21:26 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-10 08:47:59 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-10 07:08:42 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-10 09:01:10 
Re: Unpredictable nature of increment operators
"Bo Persson" &l  2008-05-11 10:56:59 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-11 09:05:09 
Re: Unpredictable nature of increment operators
Old Wolf <oldwolf@[EMA  2008-05-11 02:14:44 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-11 09:09:34 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-11 09:19:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:55:39 CDT 2008.