Reputation: 53
In the following example I expect to see
doSomething
asserting in "doSomething()"
However i dont see any output on console.
When I use HUTAssert(doSomething()) in main() , I see the expected output, so I expect this has to do with expending a macro within another macro
#include <iostream>
using namespace std;
#define LOPTAssertEnabled 1
#define HUTAssert(expr) ( (expr)? (void(0)) : assertMe(#expr) )
#define HAOPAssert(expr) ((isLOPTAssertEnabled())? HUTAssert(#expr) : void(expr))
void assertMe(char const* expr) {
std::cout <<" asserting in "<<expr;
}
bool doSomething() {
std::cout <<" did something "<<std::endl;
return false;
}
bool isLOPTAssertEnabled() {
return LOPTAssertEnabled;
}
int main() {
HAOPAssert(doSomething());
}
Upvotes: 1
Views: 83
Reputation: 10655
You can easily see what preprocessor is doing. For example, with gcc (g++) you can use the "-E" option to see what preprocessor is producing. In your case, you are getting this:
void assertMe(char const* expr) {
std::cout <<" asserting in "<<expr;
}
bool doSomething() {
std::cout <<" did something "<<std::endl;
return false;
}
bool isLOPTAssertEnabled() {
return 1;
}
int main() {
((isLOPTAssertEnabled())? ( ("doSomething()")? (void(0)) :
assertMe("\"doSomething()\"") ) : void(doSomething()));
}
I believe, this is not what your are expecting. However, if you strip off #
, which is "stringfying" your token, from HUTAssert(#expr)
, I believe it will be close to what you are expecting
Upvotes: 4