Master the use of backslash continuation in C++ programming

  • 2020-05-07 20:07:26
  • OfStack

1) used in macro definitions:


#define CV_ARE_SIZES_EQ(mat1, mat2) \
  ((mat1)->rows == (mat2)->rows && (mat1)->cols == (mat2)->cols)

2) used in printf. Sometimes the sentences in printf are too long and need to be shred.
3) use "//" to comment only the current line. If you want to comment out the next line, you can put a backslash at the end of the line.
Also, the backslash has the meaning of an escape character in addition to a line break. For example, "\ n "means newline, "\t" "\b", etc., the backslash means escape, and the symbol behind the backslash means escape.
However, to take the backslash's original meaning, you need to add another backslash before the backslash to represent it correctly. For example, if I want to read F:\ OpenCV2.0 \vs2008\ videos1.avi, I cannot write this directly, but should add one more slash before each backslash to say: F:\\ OpenCV2.0 \\vs2008\\videos\\ videos1.avi, so as to read the file you want correctly.
In conclusion 1, there are two functions of backslashes that I have known so far:
1 is an escape character, and the operation to be performed is the operation of the character immediately following it.
Combination of 2 and the enter key for forced line feed. Enter a backslash where you want to force a line break and press enter. When the system compiles, the line below the backslash and the line in front of it are automatically interpreted as a statement.

line continuation operator
adds one line after the normal line (VC automatically determines whether the line continues), but it is especially useful in macro definitions, which specify that they must be completed in one line:


 #define SomeFun(x, a, b) if(x)x=a+b;else x=a-b;

There is no problem with the one-line definition, but the code is not easy to understand, and it will be troublesome to maintain in the future. If written as:


#define SomeFun(x, a, b)
 if (x)
  x = a + b;
else
  x = a - b;

  is easy to understand, but the compiler makes an error because it thinks #define SomeFun(x, a, b) is the entire 1 line,if (x) and the following statements have nothing to do with #define SomeFun(x, a, b).
This is where we have to write it:


 #define SomeFun(x, a, b)\
if (x)\
x = a + b;\
else\
x = a - b;

Note: don't add a line continuation to the last line.VC's preprocessor automatically removes the \ and newline return before compiling so that the 1 doesn't affect reading or logic


Related articles: