Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to have a block of c++ code with a backslash after each semicolon?

Tags:

c++

syntax

I have recently seen blocks of C++ code where there is a "\" after each semicolon. It seems very odd to me. Perhaps it is nothing more than a mistake or the remnants of some long forgotten comments (although those have a forward slash "/" ). What impact would this "\" have on the code?

Her is a code sample.

#define PE_DECLARE_CLASS(class_) \
typedef class_ MyClass; \
static void setSuperClasses(); \
like image 826
Gandalf458 Avatar asked Jan 28 '13 20:01

Gandalf458


People also ask

What does back slash mean in C?

In Windows systems, for example, the backslash is used to separate elements of a file path, for example: C:\Documents\User\File. In C, Perl and Unix scripting, the backslash indicates that the following character must be treated in some special way. Within the TeX typesetting markup system, the backslash starts tags.

What is the use of backslash in C++?

The backslash character ( \ ) is a line-continuation character when it's placed at the end of a line. If you want a backslash character to appear as a character literal, you must type two backslashes in a row ( \\ ). For more information about the line continuation character, see Phases of Translation.

What does double slash mean in C?

The double slash tells the compiler to ignore everything that follows, until the end of the line. Multiline comments are started by using a forward slash followed by an asterisk (/*). This “slash-star” comment mark tells the compiler to ignore everything that follows until it finds a “star-slash” (*/) comment mark.

What does double backslash mean in C++?

Since the double backslash is inside a string literal, its meaning is clear: it resolves to a single \ character in the string. The strings continues with a literal newline, which makes the source not syntactically valid C++, and which is also why syntax highlighting stops at that point.


1 Answers

A backslash as last character in a line causes this line to be joined with the next for preprocessing. For regular C++ parsing newlines are simply whitespace, so this does not matter. But preprocessor directives, in particular macro definitions end at the end of line.

Using a backslash for line continuation allows formatting long macro bodies across multiple source text lines.

like image 73
JoergB Avatar answered Nov 07 '22 02:11

JoergB