f



inline function argument evaluation

I would like to implemenent an unconditional or in the following way

    inline bool uor(bool a, bool b) { return a || b; }

such that, for instance wit the following function

    bool doit(int& count)
    {
        ++count;
        return true;
    }

'count' after

    int count = 0;
    bool result = uor(doit(count), doit(count));

would be 2

whereas, normally after

    int count = 0;
    bool result = doit(count) || doit(count);

'count' would be 1

My question is: Do there exist C++ compilers that would (in my opinion
incorrectly) optimize out the evaluation of the second argument of 'uor'
and
make it a conditional or?

Thanks,

Gino



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Gino
6/30/2003 4:04:10 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

3 Replies
571 Views

Similar Articles

[PageSpeed] 28

"Gino van den Bergen" <gino@dtecta.COMAGAIN> wrote in message news:78122$3f0003fb$d468cfd2$19489@nf1.news-service.com...

>
> My question is: Do there exist C++ compilers that would (in my opinion
> incorrectly) optimize out the evaluation of the second argument of 'uor'
> and
> make it a conditional or?
>
Not any that aren't seriously broken.   No I haven't seen any.
Even operator|| overloads are forced to evaluation both arguments.



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Ron
6/30/2003 6:52:59 PM
 > > My question is: Do there exist C++ compilers that would (in my opinion
 > > incorrectly) optimize out the evaluation of the second argument of 'uor'
 > > and
 > > make it a conditional or?
 > >
 > Not any that aren't seriously broken.   No I haven't seen any.
 > Even operator|| overloads are forced to evaluation both arguments.

However, it is important to note that the evaluation order of the
arguments when calling any function (inline or not) is unspecified.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
emild
7/1/2003 8:14:13 AM
>Not any that aren't seriously broken.   No I haven't seen any.
>Even operator|| overloads are forced to evaluation both arguments.
>
Steve Myers mentions this in one of his Effective C++ books, although
he sees the problem from the other viewpoint.  

He argues that programmers are so used to short circuit evalution, you
should not overload || or && as this forces the compiler to always
evaluate both options.

Matt

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Matthew
7/2/2003 4:58:49 PM
Reply: