当一行中只有一个“#”而没有其他任何内容时,C++ 预处理器会做什么?

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

这是有效的 C++ 行吗?它应该是什么意思?

#

这个怎么样:

# // a comment

最近的编译器似乎忽略了它,没有错误也没有警告。

是"do nothing"吗?我有一个头文件,其中 "compatibility" 部分在存在此类行的情况下使用 g++ 7.4.0 编译时会爆炸。它似乎并没有绊倒看到这一行被排除在非活动 #if 分支中的编译器。

注意:在 Travis CI 上的 Debian Bionic(截至撰写本文时)的 gcc 7.4.0 被这些行绊倒了。

两个例子都有效,但它们什么都不做。

第二个例子,先把translation phase 3中的注释去掉:

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

这导致第一种情况,它是在 translation phase 4:

中扩展的预处理器指令

Preprocessing directives are executed, ...

此预处理器指令有效,称为 Null directive,但没有任何效果,如 here:

所述

A preprocessing directive of the form

# new-line

has no effect.

其中 new-line 字面意思是换行符。

所以您显示的代码是有效的,并且应该被一致的实现所接受。