Re: UB?
While the statement (--i)++ results in undefined behavior when the
variable i is an integer, is the same true if the variable i is a class
for which there are overloaded prefix decrement and postfix increment
operators?
struct Integer
{
int x;
Integer( int x_ ) : x( x_ ) {}
operator int( void ) const
{
return x;
}
};
Integer& operator --( Integer& i )
{
--i.x;
return i;
}
Integer operator ++( Integer& i, int )
{
Integer original( i );
i.x++;
return original;
}
int A( void )
{
Integer i = 0;
while( operator ++( operator --( i ), 0 ) );
return i;
}
I assume that, in the execution of function A, there is a sequence
point before the execution of the postfix increment operation. This
sequence points necessitates that the prefix decrement operation be
complete before the beginning execution of the postfix increment
operation. Consequently, the behavior of function A is well-defined.
int B( void )
{
Integer i = 0;
while( (--i)++ );
return i;
}
Does the statement (--i)++ in function B break down into two function
calls akin to what is in function A? If so, does this insert a
"hidden" sequence point in the execution of function B that makes its
behavior well-defined?
-----
Matthew T. Staebler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]