gpt4 book ai didi

c++ - 我的头文件是否应该能够自行编译?

转载 作者:行者123 更新时间:2023-12-02 09:55:33 28 4
gpt4 key购买 nike

一位同事最近声称,头文件应始终能够自行编译(例如,调用g++ someHeader.hpp会导致someHeader.hpp.gch正常运行)。

这句话是真的吗?

检查 header 是否能够自行编译是一种好/常见的做法吗?

如果是,这样的测试有什么好处?

在我看来,这似乎是违反直觉的,因为 header 由编译器包含在编译单元中,并且仅需在该单元的上下文中进行编译。

最佳答案

Is it a good/common practice to check if your headers are able to compile on their own?



是。据我所知,这种做法是好的,很普遍。

If yes, what would be the advantage



不能单独工作的 header 必须依赖于将要包含它的上下文。
仅当包含在特定上下文中时才具有 header 工作是脆弱的。脆性是一种代码气味。

... headers are included by the preprocessor in the compilation unit and only have to compile in the context of that unit.



...以及包含 header 的所有其他单元。包括那些尚未写入的单元。而且,当对这些单元进行更改时,它也应继续工作。

从长远来看,仅在一个时间点在单个翻译单元中工作的 header 不是一个非常有用的 header 。

关于c++ - 我的头文件是否应该能够自行编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60440553/

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