如果 header 已经包含在预编译的 header 中,我是否应该继续在我的源文件中包含它们?

Should I continue to include headers in my source file if they are already included in the precompiled header?

令人惊讶的是,我发现了唯一 真正 类似的问题(Advantages of removing includes which are already in precompiled header?) only asks for possible compile time gains, and mentions there are disadvantages but without specifying what these might be. There are other related 但这些问题包括 headers 在 headers而不是直接在翻译单元中,所以我想具体问一下。

考虑:

precompile.hpp

#ifndef PRECOMPILE_H
#define PRECOMPILE_H

#include <string>
#include <iostream>

#endif // PRECOMPILE_H

precompile.cpp

#include "precompile.hpp"

main.cpp

#include "precompile.hpp"

#include <string>
#include <iostream>

int main()
{
    std::string str = "hello";
    std::cout << str << "\n";
}

关于是否在源文件中显式包含预编译的 headers(<string><iostream> 在我的例子中)?

您可能会争辩说任何源文件都应该 #include 它所依赖的一切,而不管预编译的是什么。

这种方法可以更轻松地在其他项目中重用代码,这些项目可能没有预编译这些依赖项。