Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

if with no code block parenthesis

5.00/5 (2 votes)
17 Feb 2011CPOL 7.6K  
Whatever, I agree with you, it would be worse in C / C++ code using macros.Suppose you got a multiline macro like this:#define some_macro do_something_1(); do_something_2();And using this macro without parenthesis:if(...) some_macro;orfor(...) ...
Whatever, I agree with you, it would be worse in C / C++ code using macros.
Suppose you got a multiline macro like this:

#define some_macro \
    do_something_1(); \
    do_something_2();

And using this macro without parenthesis:
if(...)
    some_macro;

or
for(...)
    some_macro;

Only do_something_1() will be executed internally in those if / for blocks and do_something_2() is treated as an external statement.
So I'm used to add a pair of { } there, and even surround multiline macros with them in C / C++ like:
#define some_macro \
    { \
        do_something_1(); \
        do_something_2(); \
    }

That would be better I think.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)