c - 在 C 头文件中将注释放在首位有什么坏处吗?

标签 c performance c-preprocessor

在现代编译器上,将注释放在头文件的开头是否有任何有意义的缺点?

也就是说,great_header.h 中的内容类似于以下内容:

/*
 * this file defines the secret to life
 * etc
 * (c) 2017 ascended being
 */

#pragma once
#ifndef NAMESPACE_GREAT_HEADER_H_
#define NAMESPACE_GREAT_HEADER_H_

... (actual contents)

#endif // ifndef NAMESPACE_GREAT_HEADER_H_

在过去,我记得诸如“#pragma once only first if it is the first line in the file”之类的警告,以及类似的 include-guard 优化规则 - 但我不记得了确定是否仍然如此。如果评论可以放在文件的第一位,这对我和提取标题顶部信息的自动化工具来说会很方便。

最佳答案

根据 GCC Preprocessor Internals手动,多重包含优化机制不受评论影响:

The Multiple-Include Optimization

Under what circumstances is such an optimization valid? If the file were included a second time, it can only be optimized away if that inclusion would result in no tokens to return, and no relevant directives to process. Therefore the current implementation imposes requirements and makes some allowances as follows:

  1. There must be no tokens outside the controlling #if-#endif pair, but whitespace and comments are permitted.

它没有提到 #pragma once ,我怀疑这是分开处理的。引用2.5 Alternatives to Wrapper #ifndef :

Another way to prevent a header file from being included more than once is with the ‘#pragma once’ directive. If ‘#pragma once’ is seen when scanning a header file, that file will never be read again, no matter what.

关于c - 在 C 头文件中将注释放在首位有什么坏处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42033338/

相关文章:

c - 并行调用函数是否足以让它并行执行?

c - 如何从 C 中的 Unix 终端获取输入文件名?

c++ - ncurses:移动/调整窗口大小后 wgetch 无法正确读取

c - 为什么 fgets 卡在回车\r 上?

javascript - 在 javascript 中使用 switch 语句而不是一堆 if()else if() 是否有任何性能提升?

perl - 如何有效地计算覆盖 Perl 给定范围的范围?

hash - 字符串的编译时(预处理器)散列

c - 如何在编译时检查 liblua 版本?

c++ - 我对 Include Guards 有一些困惑

c - 需要用递归的方法来解决这个问题