www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why isn't this expression const?

reply Brian Palmer <d brian.codekitchen.net> writes:
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
next sibling parent reply Silverling <este_aqui_ hotmail.com.remove.underscores> writes:
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
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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 09 2007
parent reply Brian Palmer <d brian.codekitchen.net> writes:
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 09 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
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 09 2007
prev sibling next sibling parent Don Clugston <dac nospam.com.au> writes:
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 09 2007
prev sibling next sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"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.
May 11 2007
prev sibling parent Brian Palmer <d brian.codekitchen.net> writes:
Stewart Gordon Wrote:

 "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.
That clears things up. Thanks all for the great answers, I'll just work around it for now.
May 11 2007