 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Al Guest
|
Posted: Fri Dec 15, 2006 10:10 am Post subject: UB? |
|
|
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
|
Posted: Sat Dec 16, 2006 10:10 am Post subject: 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! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sun Dec 17, 2006 12:22 am Post subject: Re: UB? |
|
|
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
|
Posted: Sun Dec 17, 2006 6:17 pm Post subject: Re: UB? |
|
|
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
|
Posted: Mon Dec 18, 2006 12:32 am Post subject: Re: UB? |
|
|
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 |
|
 |
|
|
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
|
|