Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Validity of several ++operators on the same line (http://www.velocityreviews.com/forums/t459854-validity-of-several-operators-on-the-same-line.html)

Kufa 01-16-2007 12:25 PM

Validity of several ++operators on the same line
 
Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );


They compiled in Commeau, but that doesn't mean it's legal :)
Any help or suggestion is welcome!
Thanks,

/kufa


Kai-Uwe Bux 01-16-2007 12:38 PM

Re: Validity of several ++operators on the same line
 
Kufa wrote:

> I am wondering of the validity of those lines, not meaning i'm using
> them, but i cant point out in the norm if they are legal, and in such a
> case the expected behaviour.
>
> int a = 2;
> int b = (++a) * ((++a)+2);
> printf( "%d %d", ++b, ++b );


The behavior is undefined as per clause [5/4] of the standard.


Best

Kai-Uwe Bux

=?iso-8859-1?q?Erik_Wikstr=F6m?= 01-16-2007 12:41 PM

Re: Validity of several ++operators on the same line
 
On Jan 16, 1:25 pm, "Kufa" <pota...@gmail.com> wrote:
> Hello,
>
> I am wondering of the validity of those lines, not meaning i'm using
> them, but i cant point out in the norm if they are legal, and in such a
> case the expected behaviour.
>
> int a = 2;
> int b = (++a) * ((++a)+2);
> printf( "%d %d", ++b, ++b );
>
> They compiled in Commeau, but that doesn't mean it's legal :)
> Any help or suggestion is welcome!


No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.

--
Erik Wikström


Kufa 01-16-2007 12:46 PM

Re: Validity of several ++operators on the same line
 

> No, they are not legal. If in doubt just try to evaluate the expression
> in your head. Remember that there are no rules that say the just
> because A comes before B in an expression A will be evaluated first.


Well i'd think this is legal with undefined behaviour. Can you point me
somewhere in the norm that'd say it's not legal, really can't find
anything.

Thanks,

/kUfa


Rolf Magnus 01-16-2007 12:50 PM

Re: Validity of several ++operators on the same line
 
Kufa wrote:

> Hello,
>
> I am wondering of the validity of those lines, not meaning i'm using
> them, but i cant point out in the norm if they are legal, and in such a
> case the expected behaviour.
>
> int a = 2;



> int b = (++a) * ((++a)+2);


This one has undefined behavior, because a is modified twice without a
sequence point in between.

> printf( "%d %d", ++b, ++b );


Undefined too, similar to the previous one.

> They compiled in Commeau, but that doesn't mean it's legal :)


Well, it's not a syntacital error. A compiler doesn't need to complain about
it.



Kufa 01-16-2007 12:59 PM

Re: Validity of several ++operators on the same line
 
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa


Andre Kostur 01-16-2007 02:55 PM

Re: Validity of several ++operators on the same line
 
"Kufa" <potages@gmail.com> wrote in news:1168952389.509578.101930@
38g2000cwa.googlegroups.com:

> So to sum up and check i did not misunderstand you, it's legal, but
> undefined behaviour ?


That depends on what you define as "legal". In my definition, "legal" code
doesn't invoke Undefined Behaviour. If by "legal" you mean "syntactically
correct", then yes it's legal.

red floyd 01-16-2007 03:43 PM

Re: Validity of several ++operators on the same line
 
Kufa wrote:
> So to sum up and check i did not misunderstand you, it's legal, but
> undefined behaviour ?
>
> Thanks for your answers!
>
> /Kufa
>

Kai-Uwe Bux already told you where to look in the standard. 5/4.

If you are unfamiliar with that notation, it means paragraph 4 of
section 5, not section 5.4.

Noah Roberts 01-16-2007 04:34 PM

Re: Validity of several ++operators on the same line
 

Kufa wrote:
> > No, they are not legal. If in doubt just try to evaluate the expression
> > in your head. Remember that there are no rules that say the just
> > because A comes before B in an expression A will be evaluated first.

>
> Well i'd think this is legal with undefined behaviour.


"Undefined behavior" means the program is not well formed, hense
illegal.


Pete Becker 01-16-2007 05:58 PM

Re: Validity of several ++operators on the same line
 
Noah Roberts wrote:
> Kufa wrote:
>>> No, they are not legal. If in doubt just try to evaluate the expression
>>> in your head. Remember that there are no rules that say the just
>>> because A comes before B in an expression A will be evaluated first.

>> Well i'd think this is legal with undefined behaviour.

>
> "Undefined behavior" means the program is not well formed, hense
> illegal.
>


Does that mean I'll be arrested for writing code like that?

Seriously, according to the standard, programs are well-formed or
ill-formed, and some well-formed programs have undefined behavior. It
doesn't say anything about legal or illegal. As always, using undefined
terms leads to confusion.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)


All times are GMT. The time now is 04:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.