C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

UB?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Al
Guest





PostPosted: Fri Dec 15, 2006 10:10 am    Post subject: UB? Reply with quote



Hi,

Does the following invoke undefined behavior?

int main() {
int i = 0;
while ((++i)--);
return 0;
}

Do parentheses affect sequence points?


Thanks,
-Al.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthew T. Staebler
Guest





PostPosted: Sat Dec 16, 2006 10:10 am    Post subject: Re: UB? Reply with quote



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! ]
Back to top
Carlos Moreno
Guest





PostPosted: Sun Dec 17, 2006 12:22 am    Post subject: Re: UB? Reply with quote



Matthew T. Staebler wrote:
Quote:
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?

No. The behaviour is perfectly defined in that case.

In some cases, however, you get unspecified behaviour, when the
order of evaluation of parameters to functions can affect the
outcome --- but I don't think this is the case, since there is
no way to call the postfix ++ operator without first having the
result from the prefix -- operator. The order of evaluation is
forced by the expression, in this particular case.

Carlos
--

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthew T. Staebler
Guest





PostPosted: Sun Dec 17, 2006 6:17 pm    Post subject: Re: UB? Reply with quote

Carlos Moreno wrote:
Quote:
Matthew T. Staebler wrote:
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?

No. The behaviour is perfectly defined in that case.

In some cases, however, you get unspecified behaviour, when the
order of evaluation of parameters to functions can affect the
outcome --- but I don't think this is the case, since there is
no way to call the postfix ++ operator without first having the
result from the prefix -- operator. The order of evaluation is
forced by the expression, in this particular case.

What would the ramifications be of treating all operators as if they
involved an implicit function call regardless of whether the operator
is a built-in operator or a user-defined operator? Would such a
modification impractically hamper a compiler's ability to optimize
code?

-----
Matthew T. Staebler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Kanze
Guest





PostPosted: Mon Dec 18, 2006 12:32 am    Post subject: Re: UB? Reply with quote

Matthew T. Staebler wrote:
Quote:
Carlos Moreno wrote:
Matthew T. Staebler wrote:
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?

No. The behaviour is perfectly defined in that case.

In some cases, however, you get unspecified behaviour, when the
order of evaluation of parameters to functions can affect the
outcome --- but I don't think this is the case, since there is
no way to call the postfix ++ operator without first having the
result from the prefix -- operator. The order of evaluation is
forced by the expression, in this particular case.

What would the ramifications be of treating all operators as if they
involved an implicit function call regardless of whether the operator
is a built-in operator or a user-defined operator? Would such a
modification impractically hamper a compiler's ability to optimize
code?

It depends on who you ask. I've been told by some of the best
specialists in optimization technology that imposing a rigorous
order, as in Java, would have no noticeable effect. I've also
been told, by other people actually working on production
compilers, that imposing such total ordering would have a
significant effect. My own knowledge of optimizing techniques
suggest that the first is true, but it does require more work on
the part of the optimizer to obtain equivalent results.

Just imposing a sequence point after each operator would
probably have similar effects on the optimization possibilities,
without offering nearly as many benefits for the programmer as
full ordering. It would not, for example, be sufficient to
make: "f( auto_ptr< T >( new T ), auto_ptr< U >( new U ) )"
work.

--
James Kanze (Gabi Software) email: james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.