gpt4 book ai didi

c++ - 在#include 语句中使用垃圾字符没有编译器错误

转载 作者:可可西里 更新时间:2023-11-01 16:35:28 25 4
gpt4 key购买 nike

#include <iostream> gfhgfhgf
using namespace std;

int main() {
return 0;
}

为什么这个代码片段可以编译?根据 The gcc reference on Include Syntax :

It is an error if there is anything (other than comments) on the line after the file name.

这正是代码中所做的。

最佳答案

gccclang 中使用 -pedantic-errors 标志将其转换为错误 see it live :

error: extra tokens at end of #include directive
#include <iostream> gfhgfhgf
^

这表明它是一个扩展。

如果我们看一下 Interfacing C and TAL In The Tandem Environment他们有这样的代码:

#include <stdlibh> nolist
^^^^^^

因此 gccclang 都支持 include 指令后的额外字符,以支持某些平台所需的扩展。使用 -pedantic flags使 gccclang 对违反标准的扩展产生警告,如上所述,您可以使用 -pendatic-errors 将其转换为错误(强调我的):

to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

我们可以在 HP'sC/C++ Programmers guide for NonStop Systrms 中找到 nolist 扩展的引用其中说:

nolist
directs the compiler not to list the contents of the file or sections
being included.
This is an HP NonStop extension to the standard.

请注意,draft C++ standard16.2 [cpp.include] 部分定义这种形式的 include 的语法如下:

# include < h-char-sequence> new-line

关于c++ - 在#include 语句中使用垃圾字符没有编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27817081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com