x=x++ -- undefined behaviour?



 DEVELOP > c-Plus-Plus > x=x++ -- undefined behaviour?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER