Thread overview
Why isn't this expression const?
May 09, 2007
Brian Palmer
May 09, 2007
Silverling
May 09, 2007
Frits van Bommel
May 10, 2007
Brian Palmer
May 10, 2007
Daniel Keep
May 10, 2007
Don Clugston
May 11, 2007
Stewart Gordon
May 09, 2007
This code won't compile:

 struct Vector3 { float x,y,z; }
 const Vector3 ROT_FORCE_CCW = {0, 400_000, 0};
 const Vector3 ROT_FORCE_CW  = {0, ROT_FORCE_CCW.y*-1, 0};

That last line gives me the error:

 Error: non-constant expression *((& ROT_FORCE_CCW+4)) * -1.0e+0F

Strangely, I can do the equivalent three lines with basic types like ints just fine, but I can't get this struct initialization to work. Am I doing something wrong, or is it just lack of compiler smarts? I'm using (gdc 0.23, using dmd 1.007)
May 09, 2007
Brian Palmer Wrote:

> This code won't compile:
> 
>  struct Vector3 { float x,y,z; }
>  const Vector3 ROT_FORCE_CCW = {0, 400_000, 0};
>  const Vector3 ROT_FORCE_CW  = {0, ROT_FORCE_CCW.y*-1, 0};
> 
> That last line gives me the error:
> 
>  Error: non-constant expression *((& ROT_FORCE_CCW+4)) * -1.0e+0F
> 
> Strangely, I can do the equivalent three lines with basic types like ints just fine, but I can't get this struct initialization to work. Am I doing something wrong, or is it just lack of compiler smarts? I'm using (gdc 0.23, using dmd 1.007)

Try
const Vector3 ROT_FORCE_CCW = [0, 400_000, 0];
const Vector3 ROT_FORCE_CW  = [0, ROT_FORCE_CCW.y*-1, 0];

That's how you init an array. I assume that Vector3 is an alias for float[3].
May 09, 2007
Silverling wrote:
> Brian Palmer Wrote:
> 
>> This code won't compile:
>>
>>  struct Vector3 { float x,y,z; }
>>  const Vector3 ROT_FORCE_CCW = {0, 400_000, 0};
>>  const Vector3 ROT_FORCE_CW  = {0, ROT_FORCE_CCW.y*-1, 0};
>>
>> That last line gives me the error:
>>
>>  Error: non-constant expression *((& ROT_FORCE_CCW+4)) * -1.0e+0F
>>
>> Strangely, I can do the equivalent three lines with basic types like ints just fine, but I can't get this struct initialization to work. Am I doing something wrong, or is it just lack of compiler smarts? I'm using (gdc 0.23, using dmd 1.007)

I can't get it to compile for ints either...

> Try
> const Vector3 ROT_FORCE_CCW = [0, 400_000, 0];
> const Vector3 ROT_FORCE_CW  = [0, ROT_FORCE_CCW.y*-1, 0];
> 
> That's how you init an array. I assume that Vector3 is an alias for float[3].

It's not an array. Look at the first line of his code: it's a struct.
May 10, 2007
Frits van Bommel Wrote:

> Silverling wrote:
> > Brian Palmer Wrote:
> > 
> >> ...
> 
> I can't get it to compile for ints either...

Sorry, now that I read what I wrote I wasn't clear. I didn't mean changing float to int, I meant doing:

 const int a = 5;
 const int b = a*-1;

works as expected.

> 
> > Try
> > const Vector3 ROT_FORCE_CCW = [0, 400_000, 0];
> > const Vector3 ROT_FORCE_CW  = [0, ROT_FORCE_CCW.y*-1, 0];
> > 
> > That's how you init an array. I assume that Vector3 is an alias for float[3].
> 
> It's not an array. Look at the first line of his code: it's a struct.

Yes, thanks.

May 10, 2007

Brian Palmer wrote:
> Frits van Bommel Wrote:
> 
>> Silverling wrote:
>>> Brian Palmer Wrote:
>>>
>>>> ...
>> I can't get it to compile for ints either...
> 
> Sorry, now that I read what I wrote I wasn't clear. I didn't mean changing float to int, I meant doing:
> 
>  const int a = 5;
>  const int b = a*-1;
> 
> works as expected.
> 
>>> Try
>>> const Vector3 ROT_FORCE_CCW = [0, 400_000, 0];
>>> const Vector3 ROT_FORCE_CW  = [0, ROT_FORCE_CCW.y*-1, 0];
>>>
>>> That's how you init an array. I assume that Vector3 is an alias for float[3].
>> It's not an array. Look at the first line of his code: it's a struct.
> 
> Yes, thanks.

Look at the error you got: the compiler is converting ROT_FORCE_CCW.y into *(&ROT_FORCE_CCW + 4), and since pointers can't be used at compile-time, the evaluation fails.

A workaround might be to store 400_000 in a named constant, and use that instead.

const ROT_FORCE = 400_000.;
const Vector3 ROT_FORCE_CCW = {0, ROT_FORCE, 0};
const Vector3 ROT_FORCE_CW  = {0, -ROT_FORCE, 0};

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
May 10, 2007
Brian Palmer wrote:
> This code won't compile:
> 
>  struct Vector3 { float x,y,z; }
>  const Vector3 ROT_FORCE_CCW = {0, 400_000, 0};
>  const Vector3 ROT_FORCE_CW  = {0, ROT_FORCE_CCW.y*-1, 0};
> 
> That last line gives me the error:
> 
>  Error: non-constant expression *((& ROT_FORCE_CCW+4)) * -1.0e+0F
> 
> Strangely, I can do the equivalent three lines with basic types like ints just fine, but I can't get this struct initialization to work. Am I doing something wrong, or is it just lack of compiler smarts? I'm using (gdc 0.23, using dmd 1.007)

It's just lack of compiler smarts. Originally, hardly any constant folding worked.
May 11, 2007
"Brian Palmer" <d@brian.codekitchen.net> wrote in message news:f1t6en$t32$1@digitalmars.com...
> This code won't compile:
>
> struct Vector3 { float x,y,z; }
> const Vector3 ROT_FORCE_CCW = {0, 400_000, 0};
> const Vector3 ROT_FORCE_CW  = {0, ROT_FORCE_CCW.y*-1, 0};
<snip>
> Am I doing something wrong, or is it just lack of compiler
> smarts?  I'm using (gdc 0.23, using dmd 1.007)

It's the compiler getting confused.

http://d.puremagic.com/issues/show_bug.cgi?id=609

Stewart.