What does a C++ preprocessor do when there's just a single `#` in a line - and nothing else?

Is this a valid line of C++? What is it supposed to mean?

#

What about this one:

# // a comment

Recent compilers seem to ignore it without errors nor warnings.

Does it "do nothing"? I have a header file where a "compatibility" section bombs out when compiled with g++ 7.4.0 in presence of such lines. It doesn't seem to trip up compilers that see this line excluded in an inactive #if branch.

Note: gcc 7.4.0 on Debian Bionic (as of this writing) on Travis CI is tripped by such lines.

Upvotes: 3

Views: 224

Answers (1)

cigien
cigien

Reputation: 60308

Both examples are valid, and they do nothing.

For the second example, first the comments are removed in translation phase 3:

... Each comment is replaced by one space character. New-line characters are retained. ...

which results in the first case, which is a preprocessor directive that is expanded in translation phase 4:

Preprocessing directives are executed, ...

This preprocessor directive is valid, and is called a Null directive, and has no effect, as stated here:

A preprocessing directive of the form

# new-line

has no effect.

where new-line is literally the new-line character.

So the code you have shown is valid, and should be accepted by a conforming implementation.

Upvotes: 8

Related Questions