gpt4 book ai didi

c++ - #include guard 在评论 block 之前还是之后?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:30 24 4
gpt4 key购买 nike

我在某处读到(抱歉,找不到链接)头文件的第一行应该始终是#include 保护,因为编译器可以在不打开头文件的情况下看到它。因此,如果一个头文件已经被包含,它不会打开文件只是为了再次关闭它,这加快了构建过程。
但我总是在每个文件的开头都有一个注释 block 。所以我的问题是,#include 守卫应该写在评论 block 之前还是之后?

这种风格是不是比较好:

///////////////////////
// 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

因此,对于大型项目的构建时间来说,打开一个文件来解析包含防护应该可以忽略不计。

我的意思是它不可能成为程序的瓶颈!

所以选择你喜欢的任何一种风格,它真的是基于意见的。


发现了另一个有趣的评论 here :

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 .

关于c++ - #include guard 在评论 block 之前还是之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47452234/

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