Path: utzoo!attcan!uunet!cs.utexas.edu!mailrus!iuvax!purdue!decwrl!hplabs!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: <6590266@hplsla.HP.COM> Date: 26 Sep 89 22:59:57 GMT References:Organization: HP Lake Stevens, WA Lines: 121 >Alan: >If a boolean type is required, it can easily be provided in C++, with whichever >properties are desired. Some people like, for instance to have: > >enum boolean { false, true, maybe }; Two problems if/when people make their own "bool" classes. First, one is forced to explicetely state those expressions one wants to be bool, otherwise they will still be "int" -- if (a&b | c&d) {dosomething();} will still follow the built-in "int" rules of evaluation, unless one somehow forces the compiler to recognize part of the subexpressions as "bool." One way to do this: if (bool(a&b) | bool(c&d)) {dosomething();} Or one could declare and evaluate a, b, c, and d ahead of time: bool a=something, b=something, c=something, d=something; ... if (a&b | c&d) {dosomething();} would also work. But who would do this compared to the simplicety of "C" "int" evaluation of boolean expressions? Also, the poor reader of this software is going to be darn hard pressed to discover whether "int" rules or "bool" rules of evaluation are in effect in a given expression. IE, the meaning of: if (a&b | c&d) {dosomething();} is going to depend on whether a, b, c, d have been declared int or bool somewhere earlier in the program. Also, present generation compilers insist on making class objects ["C" structures] memory resident, as oppose to allowing them to exist solely in registers. This makes a bool class several times slower than using built-in ints in boolean expressions. Compare the efficiency of the int verses "bool" expressions as compiled on my m680x0 machine: ------------------------------------------------ class bool { int b; public: bool():b(0){} bool(int a):b(a){} friend bool operator|(const bool& a, const bool& b) {return a.b | b.b;} friend bool operator&(const bool& a, const bool& b) {return a.b & b.b;} }; int intexpr(const int a, const int b, const int c, const int d) {return (a&b) | (c&d);} bool boolexpr(const bool& a, const bool& b, const bool& c, const bool& d) {return (a&b) | (c&d);} ------------------------------------------------ Compiles to [AT&T 2.0, HP backend C compiler]: ------------------------------------------------ global intexpr(int,int,int,int) intexpr(int,int,int,int) : link.w %a6,&0 mov.l 8(%a6),%d0 and.l 12(%a6),%d0 mov.l 16(%a6),%d1 and.l 20(%a6),%d1 or.l %d1,%d0 unlk %a6 rts global boolexpr(const bool&,const bool&,const bool&,const bool&) boolexpr(const bool&,const bool&,const bool&,const bool&) : link.w %a6,&-40 mov.l %a1,-4(%a6) mov.l 8(%a6),-8(%a6) mov.l 12(%a6),-12(%a6) mov.l -8(%a6),%a0 mov.l (%a0),%d0 mov.l -12(%a6),%a0 and.l (%a0),%d0 mov.l %d0,-16(%a6) lea -16(%a6),%a0 mov.l (%a0),-32(%a6) mov.l 20(%a6),-(%sp) mov.l 16(%a6),-(%sp) lea -40(%a6),%a1 jsr operator&(const bool&,const bool&) addq.w &8,%sp mov.l %d0,%a0 mov.l (%a0),-36(%a6) mov.l -32(%a6),%d0 or.l -36(%a6),%d0 mov.l %d0,-28(%a6) lea -28(%a6),%a0 mov.l -4(%a6),%a1 mov.l (%a0),(%a1) mov.l -4(%a6),%d0 unlk %a6 rts operator&(const bool&,const bool&) : link.w %a6,&-8 mov.l %a1,-4(%a6) mov.l 8(%a6),%a0 mov.l (%a0),%d0 mov.l 12(%a6),%a0 and.l (%a0),%d0 mov.l %d0,-8(%a6) lea -8(%a6),%a0 mov.l (%a0),(%a1) mov.l -4(%a6),%d0 unlk %a6 rts