Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: function in...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 9 Topic 4205 of 4295
Post > Topic >>

Re: function in an expression

by "GuGu" <prompete@[EMAIL PROTECTED] > Jul 21, 2008 at 02:38 PM

>>  int& func( int& i ) { return ++i; }
>>  A = B[ func(i) ]  + B[ func(i) ];
>>
>> And they told me that write something similar is absolutely wrong.
>> Mine is just a nightmare or what they are telling me is true?
>
> Yes, that code is horrible. When reading it without seeing the
declaration
> of func() in view, I would guess that it should yield the same result
both
> times, so you can extract the call to func() and store it in a
tem****ary.

That's absolutely true, but nobody know what exactly func(i) do (what
about 
this function returns random values?). So my position is that the compiler

cannot decide to optimize in "any way" this expression because it will be
a 
huge error.

> However, it could also first invoke the left hand side of the addition
and
> then the right one:
>
>  int _1 = B[func(i)];
>  int _2 = B[func(i)];
>  A = _1 + _2;

I agree with you that this is a better way to write the same code.
 




 9 Posts in Topic:
function in an expression
"GuGu" <prom  2008-07-16 11:44:18 
Re: function in an expression
Jerry Coffin <jcoffin@  2008-07-16 06:42:17 
Re: function in an expression
"GuGu" <prom  2008-07-16 15:40:48 
Re: function in an expression
Ulrich Eckhardt <dooms  2008-07-17 00:14:06 
Re: function in an expression
"GuGu" <prom  2008-07-21 14:38:06 
Re: function in an expression
Ulrich Eckhardt <dooms  2008-07-21 21:28:48 
Re: function in an expression
"GuGu" <prom  2008-07-22 08:07:13 
Re: function in an expression
Barry Schwarz <schwarz  2008-07-16 21:27:17 
Re: function in an expression
Ben Bacarisse <ben.use  2008-07-17 12:45:37 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 16:14:06 CDT 2008.