#include guard 在评论块之前还是之后?
#include guard before or after comment block?
我在某处读到(抱歉再也找不到 link),header 的第一行应该始终是 #include 守卫,因为编译器可以在不打开的情况下看到它header 文件。因此,如果 header 文件已经包含,它不会打开文件只是为了再次关闭它,这会加快构建过程。
但我总是在每个文件的开头都有一个注释块。所以我的问题是,#include 守卫应该写在评论块之前还是之后?
这种风格比较好:
///////////////////////
// Name: code.h
// Author: Me
// Date: dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////
#ifndef CODE_H_
#define CODE_H_
...
#endif
或者这种风格更好:
#ifndef CODE_H_
#define CODE_H_
///////////////////////
// Name: code.h
// Author: Me
// Date: dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////
...
#endif
或者根本不重要?
根据我的经验和 others'
is that it's NOT the parsing and reading of files that take the time, but the various optimisation and code-generation passes
因此,对于大型项目的构建时间来说,打开一个文件来解析包含防护应该可以忽略不计。
我的意思是不可能是程序的瓶颈!
所以选择你喜欢的任何一种风格,这真的是基于意见。
发现另一个有趣的评论 :
One upon a time, there might have been one or two compilers stupid enough to open the file each time to check the include guard. No compiler produced in this millennium would do that, as it can just keep a table of files and include guards and consult that before opening the file.
在 File-wide include-guards 阅读更多内容。
我在某处读到(抱歉再也找不到 link),header 的第一行应该始终是 #include 守卫,因为编译器可以在不打开的情况下看到它header 文件。因此,如果 header 文件已经包含,它不会打开文件只是为了再次关闭它,这会加快构建过程。
但我总是在每个文件的开头都有一个注释块。所以我的问题是,#include 守卫应该写在评论块之前还是之后?
这种风格比较好:
///////////////////////
// Name: code.h
// Author: Me
// Date: dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////
#ifndef CODE_H_
#define CODE_H_
...
#endif
或者这种风格更好:
#ifndef CODE_H_
#define CODE_H_
///////////////////////
// Name: code.h
// Author: Me
// Date: dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////
...
#endif
或者根本不重要?
根据我的经验和 others'
is that it's NOT the parsing and reading of files that take the time, but the various optimisation and code-generation passes
因此,对于大型项目的构建时间来说,打开一个文件来解析包含防护应该可以忽略不计。
我的意思是不可能是程序的瓶颈!
所以选择你喜欢的任何一种风格,这真的是基于意见。
发现另一个有趣的评论
One upon a time, there might have been one or two compilers stupid enough to open the file each time to check the include guard. No compiler produced in this millennium would do that, as it can just keep a table of files and include guards and consult that before opening the file.
在 File-wide include-guards 阅读更多内容。