------- Additional Comments From chris at bubblescope dot net  2004-12-08 14:15 
-------
Heres a much smaller testcase which shows this bug:

#include<stdio.h>
 
class complex
  {
  public:
    complex(long double r=0, long double i=0)
   {
    __real__ _M_value = r;
    __imag__ _M_value = i;
   }
    long double real() const
    { return __real__ _M_value; }
    long double imag() const
    { return __imag__ _M_value; }
    __complex__ long double _M_value;
  };

int main(void)
{
complex x(2,2),y,z;
int n = 1;
y = (n==1) ? x : complex(3,3);
n=2;
z = (n==1) ? x : complex(3,3);
printf("(%Lf,%Lf),(%Lf,%Lf),(%Lf,%Lf)\n",x.real(),x.imag(),y.real(),y.imag(),z.real(),z.imag());
} 


This should print (2.0,2.0),(2.0,2.0),(3.0,3.0). Instead it prints
(2.0,2.0),(2.0,0.0),(3.0,3.0).

This bug is fairly obscure: As far as I can see it only appears if:
a) Using long double complex
b) Using n ? y : z operator
c) y is a previously declared variable, z is a temporary created here.

It turns up in bits/cmath.tcc, __cmath_power. A quick hacky fix is to change the
first line of this function from:
_Tp __y = __n % 2 ? __x : 1;
to
_Tp __temp=1;
_Tp __y = __n % 2 ? __x : __temp;

If you want to get your code to work.

I confirm this bug occurs in g++ 3.3.3 (on cygwin), and despite some fiddling I
can't get a similar thing to happen with current CVS.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18882

Reply via email to