| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"kwikius" |
| Date: |
20 Jan 2008 07:57:46 AM |
| Object: |
x=x++ -- undefined behaviour? |
Is the marked section in main so-called "undefined behaviour".
struct value_type{
value_type (): v(0){}
value_type & operator = ( value_type const & in)
{
v = in.v;
return *this;
}
int v;
};
inline
value_type operator++(value_type & x,int)
{
value_type t = x;
++x.v;
return t;
}
int main()
{
value_type x;
//####################
x = x++; // undefined behaviour ??
//###############
x = (x++);
(x = x)++;
}
IMO it aint where x is a UDT with overloaded post operator ++.
But maybe I'm wrong?...
regards
Andy Little
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: x=x++ -- undefined behaviour? |
20 Jan 2008 08:29:24 AM |
|
|
* kwikius:
Is the marked section in main so-called "undefined behaviour".
struct value_type{
value_type (): v(0){}
value_type & operator = ( value_type const & in)
{
v = in.v;
return *this;
}
int v;
};
inline
value_type operator++(value_type & x,int)
{
value_type t = x;
++x.v;
return t;
}
int main()
{
value_type x;
//####################
x = x++; // undefined behaviour ??
//###############
x = (x++);
(x = x)++;
}
IMO it aint where x is a UDT with overloaded post operator ++.
But maybe I'm wrong?...
Except for the zero, which I'm too lazy to look up whether it's required
to be zero or whatever, the expression is evaluated as
x.operator=( operator++( x, 0 ) );
These function calls introduce sequence points, in particular, there is
a sequence point between evaluating the argument to operator= and
executing the operator= function body, so that all side effects must be
complete.
So the behavior is not undefined.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
| User: "kwikius" |
|
| Title: Re: x=x++ -- undefined behaviour? |
20 Jan 2008 09:10:27 AM |
|
|
On Jan 20, 2:29=A0pm, "Alf P. Steinbach" <al...@start.no> wrote:
Thanks both of you for the info.
regards
Andy Little
.
|
|
|
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: x=x++ -- undefined behaviour? |
20 Jan 2008 08:21:11 AM |
|
|
On 2008-01-20 14:57, kwikius wrote:
Is the marked section in main so-called "undefined behaviour".
struct value_type{
value_type (): v(0){}
value_type & operator = ( value_type const & in)
{
v = in.v;
return *this;
}
int v;
};
inline
value_type operator++(value_type & x,int)
{
value_type t = x;
++x.v;
return t;
}
int main()
{
value_type x;
//####################
x = x++; // undefined behaviour ??
//###############
x = (x++);
(x = x)++;
}
IMO it aint where x is a UDT with overloaded post operator ++.
But maybe I'm wrong?...
No, it is well-defined, since x++ in this case is just syntactic sugar
for x.operator++(). The standard says that there is a sequence point
before copying the return value, so all side-effects of the function
must be done before the assignment.
--
Erik Wikström
.
|
|
|
|

|
Related Articles |
|
|