Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-sdd!apollo!lsmith From: lsmith@apollo.HP.COM (Lawrence C. Smith) Newsgroups: comp.lang.c++ Subject: Re: operator functions and classes (was Re: Time to standardize "true" and "false") Message-ID: <4625b766.20b6d@apollo.HP.COM> Date: 10 Oct 89 17:34:00 GMT References: <6590293@hplsla.HP.COM> Sender: root@apollo.HP.COM Reply-To: lsmith@apollo.HP.COM (Lawrence C. Smith) Organization: Hewlett-Packard Apollo Division - Chelmsford, MA Lines: 50 In article <6590293@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes: >//here's another simple test for those who complain my previous test can >//be evaluated by a compiler completely at compile time while still generating >//bogus runtime evaluations. [Obviously, no amount of testing can "prove" a >//real compiler works right. What I'm trying to do is convince any remaining >//sceptics who still think there is a compiler that evaluates !!x to mean x >//to find it for us! [I've yet to hear from *anyone* who actually has found >//a compiler that generates bogus code for !!x. --Or maybe whoever wrote >//such a compiler is too ashamed to admit it! :-]] > I was one of the original doubters, and I've been trying a few things on some of the C compilers I have access to. The results were interesting. In general, something like: if(!!x)... compiles as: if(x) in *all* the compilers I tested. I assumed I was vindicated in my opinion. Then I tried a few other things: y = !!x; Sure enough, y was 1 when x was != 0, and 0 when it was. The verbose version of if: if(!!x == 1) compiles correctly. This was a fascinating exercise! It would appear that the optimization in question was related not to expression evaluation (which I assumed it was) but to flow control. I have worked on lots of compilers, but I'd be the first to admit that a C compiler was not among them, and I more or less assumed they worked like the Pascal/Ada/Modula family of languages as far as expression evaluation is concerned. It appears that, to the contrary, flow-control statements like if *do* get the optim- ization, but expressions do not. It is now clear that the distinction is because of cases like !!x - optimization is obvious, yet leads to incorrect code under certain circumstances. In this case, whenever the result of the expression is used as *value* and not as a *flag*. So the majority seems to be right. But I bet it's one royal bitch to write an optimizing C compiler with gotchas like that running around. Larry Smith Internet: lsmith@apollo.hp.com UUCP: {decvax,mit-eddie,umix}!apollo!lsmith