S
Stephen Tyndall
I know the preprocessor is evil, but I'd like to know what's going on in the
following code.
The problem is when the num variable is used in the ASSERT macro inside
main(). When running the code, I get the following error from Visual
C++.NET 2003:
warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to
type 'int' can equal the given constant
#define DEBUG 2
#include <iostream>
using namespace std;
#if DEBUG == 0
#define ASSERT(n)
#elif DEBUG == 1
#define ASSERT(n) if(!n) { cout << #n << " failed" << endl; }
#elif DEBUG == 2
#define ASSERT(n) \
if(!n) { \
cout << #n << " failed on line " << __LINE__ << endl; \
} \
else { \
cout << #n << " passed on line " << __LINE__ << endl; \
}
#endif
int main() {
bool flag = true;
int num = 5;
ASSERT(flag == true);
ASSERT(flag == false);
// when I change the below to ASSERT((num == 5)); it works perfectly.
// what's wrong with the version below?
ASSERT(num == 5);
return 0;
}
Thanks in advance for your help.
following code.
The problem is when the num variable is used in the ASSERT macro inside
main(). When running the code, I get the following error from Visual
C++.NET 2003:
warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to
type 'int' can equal the given constant
#define DEBUG 2
#include <iostream>
using namespace std;
#if DEBUG == 0
#define ASSERT(n)
#elif DEBUG == 1
#define ASSERT(n) if(!n) { cout << #n << " failed" << endl; }
#elif DEBUG == 2
#define ASSERT(n) \
if(!n) { \
cout << #n << " failed on line " << __LINE__ << endl; \
} \
else { \
cout << #n << " passed on line " << __LINE__ << endl; \
}
#endif
int main() {
bool flag = true;
int num = 5;
ASSERT(flag == true);
ASSERT(flag == false);
// when I change the below to ASSERT((num == 5)); it works perfectly.
// what's wrong with the version below?
ASSERT(num == 5);
return 0;
}
Thanks in advance for your help.