Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!wugate!uunet!ncrlnk!ncr-sd!hp-sdd!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: operator functions and classes (was Re: Time to standardize "true" and "false") Message-ID: <6590281@hplsla.HP.COM> Date: 5 Oct 89 18:38:29 GMT References: Organization: HP Lake Stevens, WA Lines: 108 >>Bart Massey >> >In article <8862@etana.tut.fi> pl@etana.tut.fi (Lehtinen Pertti) writes: >> >| Then suddenly just behind the corner cames C-compiler from >> >| ACME-corporation and realizes '!!a' -> negation of negation is >> >| same as original -> we can optimize it away. >> >| >> >| Nice, isn't it. And too real too. >> > >> >And wrong, too. Do you know of a compiler that does this in the >> >general case? >> >> Yes, all of them. > >Aiigh! I'm sure I'm about the millionth response to this, but I just >can't stand it anymore! > > "The result of the logical negation operator ! is > 1 if the value of its operand is 0, 0 if the value > of its operand is non-zero. The type of the > result is int. It is applicable to any arithmetic > type or to pointers" -- K&R A:7.2, Stroustrup r.7.2 > > "The result of the logical negation operator ! is 0 > if the value of its operand compares unequal to 0, > 1 if the value of its operand compares equal to 0. > The result has type int. The expression !E is > equivalent to (0==E)." -- ANSI X3J11/88-158 3.3.3.3 19 > >So anything that is supposed to be a standard C or C++ compiler >is *required* to do the right thing! As a test, I tried 4 C compilers >we have around: > > Stanford's V6.0 68k (an ancient PCC based compiler) > Greenhills 68k (a modern commercial compiler) > BSD 4.3Tahoe VAX (a modern PCC based compiler) > GNU GCC1.34 VAX (a cool highly optimizing retargetable compiler) > I agree with Bart, add to this list the following conforming compilers [sorry, I don't have an apollo division compiler] HP-s300 C compilers HP-s800 C compilers HP-s300 C++ 2.0 compilers HP-s300 g++ compilers [from FSF] HP-s300 gcc compilers [from FSF] Below find another program you can use to help convince yourself that 'C' and 'C++' compilers really do evaluate '!' to a zero or one value, or alternately produce code that works equivalent to this statement. For example the s300 gcc -O code produced by the following program is: #NO_APP gcc_compiled.: .text LC0: .ascii "compiler fails on !!-1\12\0" LC1: .ascii "compiler fails on !!0\12\0" LC2: .ascii "compiler fails on !!-0\12\0" LC3: .ascii "compiler fails on !!1\12\0" LC4: .ascii "compiler fails on !!-100\12\0" LC5: .ascii "compiler fails on !!100\12\0" LC6: .ascii "this compiler seems to be in conformance in its use of '!!'\12\0" .even .globl _main _main: link a6,#0 pea LC6 jbsr _printf unlk a6 rts Which clearly 'does not' 'evaluate' the multiple occurances of '!' to a 'zero or one value' -- it doesn't 'evaluate' anything, it just figures out the right result at compile time and generates a little code to print it. Which is conformance. Please warn us if anyone manages to find a compiler that isn't in conformance as tested by the following simple program: ------------------------------------------------------------------------------- #ifdef __cplusplus extern "C" {int printf(const char* const, ...);}; #endif main() { if((!!-1) != 1) {printf("compiler fails on !!-1\n");} else if((!!0) != 0) {printf("compiler fails on !!0\n");} else if((!!-0) != 0) {printf("compiler fails on !!-0\n");} else if((!!1) != 1) {printf("compiler fails on !!1\n");} else if((!!-100) != 1) {printf("compiler fails on !!-100\n");} else if((!!100) != 1) {printf("compiler fails on !!100\n");} else printf("this compiler seems to be in conformance in its use of '!!'\n"); }