Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!elan!tom From: tom@elan.elan.com (Tom Smith) Newsgroups: comp.lang.c++ Subject: Re: default argument bug in 1.2! Message-ID: <590@elan.elan.com> Date: 24 Jul 89 19:42:28 GMT References: <6409@columbia.edu> Organization: Elan Computer Group, Inc., Mountain View, CA Lines: 50 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