在现代编译器上,将注释放在头文件的开头是否有任何有意义的缺点?
也就是说,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:
- 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/