Using `else` in Macros

I saw 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 

Here is how we can use it.

 void testingMacros1 (){ double x =0.0; QL_REQUIRE (x!=0 ," Zero number !"); } 

I guess the else at the end has some special use.

Questions> What else use is added at the end of this macro definition?

thanks

+6
source share
1 answer

The macro checks the condition. This condition must be true , otherwise it will throw an exception. If this is true, you will put the brackets after the usual if .

You would use it as follows:

 QL_REQUIRE (x != 0, "x must not be 0") { y = 100 / x; //dividing by 0 is bad } 

The macro is provided, and if it does not work, it will print this message. If this does not fail, your curly braces or single-line expressions form an else statement. The logic just changed a bit if you look at all this. When used, it is similar to if , but if it is swapped, if and else will get the opposite roles.

It's kind of like saying this:

 assert (x != 0 && "x must not be 0"); y = 100 / x; //dividing by 0 is bad 
+10
source

Source: https://habr.com/ru/post/916602/


All Articles