Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ MultiLine #if

I've been trying to google this for a while now but I can't seem to be able to find any clear answer if it can be done at all.

I wanted to know if it's possible to do a MultiLine #if statement in C++ in a similar way to this type of if

if (
   1 == 1 ||
   2 == 2 ||
   3 == 3
) {
   cout << "True\n";
}

I was hoping for something along the lines of (which I know is hopelessly wrong)

#if
   1 == 1 ||
   2 == 2 ||
   3 == 3
#then
   cout << "True\n";
#else
   cout << "False\n";
#endif
like image 394
TheLovelySausage Avatar asked Nov 29 '22 13:11

TheLovelySausage


2 Answers

#if \
   1 == 1 || \
   2 == 2 || \
   3 == 3
   cout << "True\n";
#else
   cout << "False\n";
#endif

Backslash-newline combinations are stripped very early in preprocessing, even before tokenizing the input. You can use this to spread preprocessor directives across multiple physical lines.

Heck, you could theoretically even do

#i\
f 1 == 1 |\
| 2 == 2 || 3 =\
= 3

but then your colleagues might get upset with you.

like image 185
melpomene Avatar answered Dec 09 '22 03:12

melpomene


Yes. With continuation lines:

#if \
   1 == 1 || \
   2 == 2 || \
   3 == 3
   cout << "True\n";
#else
   cout << "False\n";
#endif
like image 32
PSkocik Avatar answered Dec 09 '22 01:12

PSkocik