'Macro doesn't handle bool conditions when they are "inline" but works via variable
I've written a basic macro which asserts regardless of debug or not:
#define ASSERT_ALWAYS(cond) \
do \
{ \
if (!cond) \
{ \
std::abort(); \
} \
} while(0) \
The strange thing is it compiles if I use a variable:
const bool s = myObj.anEnum != AnEnum::C;
ASSERT_ALWAYS(s);
but without the variable this doesn't compile:
ASSERT_ALWAYS(myObj.anEnum != AnEnum::C);
I get this compiler error:
error: no match for ‘operator!’ (operand type is ‘AnEnum::C’)
no known conversion for argument 1 from ‘AnEnum::C’ to ‘bool’
AnEnum is declared like this:
enum class AnEnum : char {
A = '0',
B = '2',
C
};
I get similar compiler errors for other types I try to change, so is the problem with my macro?
Solution 1:[1]
When you use the expression, the preprocessor pastes
if(!myObj.anEnum != AnEnum::C)
so the compiler processes
if( (!myObj.anEnum) != (AnEnum::C) )
and the error makes more sense.
What you need is if(!(cond))
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |