Advertisement Jump to content
Sign in to follow this  

Constant can't appear in a constant expression

This topic is 4978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Don't bother reading this if you aren't bored out of your mind (I wont even care if nobody replies). I got bored, and decided to make something to read avis (WITHOUT VFW). It works as good as I had expected (wont read compressed data, and I don't plan on making it do so), however, I did come across a small problem when I tried to rewrite the MAKEFOURCC define macro to accept strings (to increase readability and save time), and I was wondering if anybody knew why.
//the new define macro, and an example of how it works
#define CreateRCode(name,str)   const unsigned int name = (str[0] | (str[1] << 8) | (str[2] << 16) | (str[3] << 24))

//some code that belongs in a function:

//a variable

//some code that works
printf("N == RIFF?: %d\n",n == RIFF_RCODE_RIFF);

//the code that doesn't work
switch (n){
  case 0: printf("ZERO\n"); break;
  case RIFF_RCODE_RIFF: printf("RIFF\n"); break; //this line wont work

//a working alternative (like MAKEFOURCC, only cleaner):
#define CreateRCode2(name,a,b,c,d)   const unsigned int name = (a | (b << 8) | (c << 16) | (d << 24))

Incase you're wondering exactly what the compiler output is, it's this: `RIFF_RCODE_RIFF' cannot appear in a constant-expression. Also, I might as well mention that I'm using a combination of both CreateRCode and CreateRCode2, since I only use the switch statements when finding chunks that need to be discarded, but halting when there is garbage in the file, and I'd prefer not to have to use a giant if statement (since a giant switch statement is cleaner). ;)

Share this post

Link to post
Share on other sites
Case labels must be integral constant expressions. A const variable is an ICE only if it is initialized by a constant expression. But a string literal is a pointer, and pointers are not allowed in a constant expression. Thus, for example,

const int i = 'a';
switch(1) { case i: ; }

is legal, whereas

const int i = "a"[0]; // or *"a" or whatever
switch(1) { case i: ; }

is not.

Share this post

Link to post
Share on other sites
Thanks for the info. Looks like I'll have to use an if statement after all... At least I can use define macros to cut that down a bit, even if it's not as clean. ;)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!