kearns2@read.columbia.edu (Steve Kearns) (07/24/89)
Is there a workaround for the following bug?
When I declare a member function to have a default constant argument,
such as :
class bar {
foo(int joe, int bill = -1);
};
Then the definition of foo usually contains a test like the following:
if (bill == -1)
{ ...do this...}
else
{ ...do that...}
The bug is that 1.2 seems to optimize out the if (bill == ..) test
and include, in this case, only the first branch of the "if".
It seems to treat the "int bill = -1" declaration as if I was initializing
a local variable.
Does anyone know any workarounds?
-steve
hughes@ns.network.com (Jim Hughes x1676) (07/24/89)
In article <6409@columbia.edu> kearns@read.UUCP (Steve Kearns) writes: > >Is there a workaround for the following bug? > >When I declare a member function to have a default constant argument, >such as : > >class bar { > foo(int joe, int bill = -1); >}; > > >Then the definition of foo usually contains a test like the following: > > if (bill == -1) > { ...do this...} > else > { ...do that...} > >The bug is that 1.2 seems to optimize out the if (bill == ..) test >and include, in this case, only the first branch of the "if". >It seems to treat the "int bill = -1" declaration as if I was initializing >a local variable. > >Does anyone know any workarounds? >-steve I have compiled a test case, and found that this does not seem to be an error as simple as you have stated. The following complete program works correctly and prints "bill != -1". --------------------------- //test.c #include <stdio.h> class bar{ public: void foo(int joe, int bill= -1); }; void bar.foo(int joe, int bill= -1) { if (bill == -1) printf("bill == -1\n"); else printf("bill != -1\n"); } main() { bar b; b.foo(23,24); } ---------------------------- Jim hughes@network.com
kearns2@read.columbia.edu (Steve Kearns) (07/25/89)
>I have compiled a test case, and found that this does not seem to be >an error as simple as you have stated. The following complete program >works correctly and prints "bill != -1". > >--------------------------- >//test.c >#include <stdio.h> > >class bar{ >public: > void foo(int joe, int bill= -1); >}; > >void bar.foo(int joe, int bill= -1) { > if (bill == -1) printf("bill == -1\n"); > else printf("bill != -1\n"); >} > >main() { > bar b; > b.foo(23,24); >} >---------------------------- > >Jim >hughes@network.com Actually, this program does demonstrate the bug on my copy of CC (#ident "@(#)cfront:CC 1.20"). And if I look at the C code it generates using CC -F -.i <name> then I can see that bar::foo compiles to the following: #line 204 "test.c" char _bar_foo (_au0_this , _au0_joe , _au0_bill ) #line 202 "test.c" struct bar *_au0_this ; #line 204 "test.c" int _au0_joe ; #line 204 "test.c" int _au0_bill ; { #line 206 "test.c" printf ( (char *)"bill == -1\n") ; } ; I guess I need a later copy. If only I had a spare $20000... -steve
tom@elan.elan.com (Tom Smith) (07/25/89)
From article <6409@columbia.edu>, by kearns2@read.columbia.edu (Steve Kearns): > > Is there a workaround for the following bug? > > When I declare a member function to have a default constant argument, > such as : > > class bar { > foo(int joe, int bill = -1); > }; > > > Then the definition of foo usually contains a test like the following: > > if (bill == -1) > { ...do this...} > else > { ...do that...} > > The bug is that 1.2 seems to optimize out the if (bill == ..) test > and include, in this case, only the first branch of the "if". > It seems to treat the "int bill = -1" declaration as if I was initializing > a local variable. > > Does anyone know any workarounds? > -steve I observed this in Oasys' distribution of the Glockenspiel port of AT&T's 1.2.1 (version "d", I think). It was not only default values of -1 in particular, but any negative number. In addition, the amount of code removed was different for different negative values in the same function! This works, for all values: static const int NO_BILL = -1; ... void foo(int joe, int bill = NO_BILL); ... // in bar::foo if (bill == -1) { ...do this... } Note that you can still compare against -1 explicitly; it is only the function template that needs to change. Question: Why would a code generator treat negative default arguments differently from positive ones? Takes your breath away, doesn't it... Hope this helps. Thomas Smith tom@elan.com, {ames, hplabs, uunet}!elan
hughes@ns.network.com (Jim Hughes x1676) (07/26/89)
Steve: Are you sure that you don't have: if (bill= -1) { in your code? Or some variables define'd? Did you compile my program intact (without including it in another program) and test it? I have the same compiler that you have and I don't get the same line numbers or the same result. Thanks jim hughes@network.com PS I have tried to get mail to you at kearns2@columbia.edu with no joy.