Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Why do I get C1017 (invalid integer constant expression) on this line?

C++
#	if ((bool)1 == (bool)2)


From MSDN: "Constants defined using #define must have values that evaluate to an integer constant if they are used in an #if, #elif, or #else directive."

Doesn't the operator== do that?

Regards
Posted

1 solution

No. This operator does not evaluate to an integer constant. Why won't you get a C++ reference and see what this operator does? I'm even ashamed to explain that. :-) But this error message is about #define, but about #if. You don't use any defined symbols in your sample anyway.

And the expression you have written looks like gibberish not only to the preprocessor. Why casting to Boolean? It makes no sense at all. And why would you even use the preprocessor, I wonder? If you are sure you need it, read about it first:
http://www.cplusplus.com/doc/tutorial/preprocessor/[^].

—SA
 
Share this answer
 
v3
Comments
AndreFratelli 10-Jul-12 13:43pm    
Actually, the error message *is* about the #if directive, check the documentation yourself ;) "if they are used in an #if, #elif, or #else directive."

Why do you say that operator== doesn't evaluate to an integer constant? This is legal:

#if 1 == 1

The statement I have above should evaluate to bool, which is pretty much castable to int. So don't feel ashamed and explain to me why does the compiler complain about this...

Because if I don't cast to bool than it's an int comparison? 1 == 2 would be pretty obvious... However, if you have native bools (not sure if in all compilers) (bool)1 == (bool)2 is actually true! Guess what: I'm trying to check if the compiler has native bool support... Makes much sense to me.

Why do people always assume others haven't read the documentation? I've been using the preprocessor for 10+ years by now...
Sergey Alexandrovich Kryukov 10-Jul-12 20:33pm    
Do you think 10 years proofs anything at all? :-)
If you want to put forward documents having nothing to do with the subject, who will listen to you?

You can read documentation thousands of times, understand it zero times. It actually has nothing to do with preprocessor documentation.
You mix up the type of operands -- integer or bool (why casting to bool - makes no sense)) with the result of an expression: "==" returns type is bool, not int.
Understood? Think a bit before voting 1... :-)

--SA
AndreFratelli 10-Jul-12 20:42pm    
No, I don't. I was just stating that I read the documentation and I understood it. Not sure what you mean by "forward documents"...

Well, not the case.

I believe I said this in my previous post: "The statement I have above should evaluate to bool, which is pretty much castable to int. So don't feel ashamed". Are you adding anything to it?

Still don't get it why I'm casting to bool? Ok... Can I vote 1 again?

Like I said, I understood your answer. Are you understanding the question? Here's an hint:

if ((bool)1 == (bool)2) { cout << "Native bool" << endl; }

Now do the same without casting.

Edit: wrong thread.
Sergey Alexandrovich Kryukov 11-Jul-12 14:39pm    
If course they are castable, so what? :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900