I have seen the following code:
#define QL_REQUIRE(condition,message) \
if (!(condition)) { \
std::ostringstream _ql_msg_stream; \
_ql_msg_stream << message; \
throw QuantLib::Error(__FILE__,__LINE__, \
BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
} else
This is how we suppose to use it.
void testingMacros1 (){
double x =0.0;
QL_REQUIRE (x!=0 ," Zero number !");
}
I assume the else in the end has some special usage.
Question> What is the usage of else appended in the end of this macros definition?
Thank you
The macro checks the condition. It needs the condition to be
true, or else it will throw an exception. If it’s true, you’d put braces after like a normalifstatement.You’d use it like this:
The macro subs in the condition, and if it fails, it will print the given message. If it doesn’t fail, your braces or one-liner form the else statement. The logic is just a bit reversed when looking at the whole thing. When using it, it’s like an
if, but when subbing it in, theifand theelsesort of get reversed roles.It’s sort of like saying this: