digitalmars.D.learn - Why isn't this expression const?
- Brian Palmer (7/7) May 09 2007 This code won't compile:
- Silverling (5/16) May 09 2007 Try
- Frits van Bommel (3/21) May 09 2007 It's not an array. Look at the first line of his code: it's a struct.
- Brian Palmer (6/20) May 09 2007 Sorry, now that I read what I wrote I wasn't clear. I didn't mean changi...
- Daniel Keep (19/42) May 09 2007 Look at the error you got: the compiler is converting ROT_FORCE_CCW.y
- Don Clugston (3/14) May 09 2007 It's just lack of compiler smarts. Originally, hardly any constant
-
Stewart Gordon
(6/12)
May 11 2007
- Brian Palmer (2/19) May 11 2007 That clears things up. Thanks all for the great answers, I'll just work ...
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:I can't get it to compile for ints either...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].It's not an array. Look at the first line of his code: it's a struct.
May 09 2007
Frits van Bommel Wrote:Silverling wrote: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.Brian Palmer Wrote:I can't get it to compile for ints either......Yes, thanks.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
Brian Palmer wrote:Frits van Bommel Wrote: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/Silverling wrote: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.Brian Palmer Wrote:I can't get it to compile for ints either......Yes, thanks.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
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
"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
Stewart Gordon Wrote:"Brian Palmer" <d brian.codekitchen.net> wrote in message news:f1t6en$t32$1 digitalmars.com...That clears things up. Thanks all for the great answers, I'll just work around it for now.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