问题描述
我有一个预编译的头文件stdafx.h,可用于项目中的所有源文件.因此,stdafx.h中的所有标头在项目中的所有代码文件中均可用.
I have a precompiled header stdafx.h which is used in all source files in my project. Thus all headers in the stdafx.h are available in all code files in the project.
我不确定的是是否要在预编译头文件中重新包含那些东西.你们怎么看?
What I'm unsure about is whether or not to re-include stuff thats already in the precompiled header. What do you guys think?
例如
stdafx.h
#pragma once
#include <memory>
my_class.h
my_class.h
#pragma once
#include <memory> // Re-include or not that's the question. Best practice?
struct my_class
{
};
typedef std::shared_ptr<my_class> my_class_ptr;
main.cpp
#include "stdafx.h"
#include "my_class.h"
int main()
{
}
推荐答案
我将包含它,以便可以在具有不同stdafx.h的项目中重复使用标头.另一种说明方式是,每个标头都应包含所有声明(最好是前者)它自己需要
I would include it so that the header could be reused in a project which has different stdafx.h Another way of stating this is each header should contain all the declarations (preferably forward ones) it needs on its own
由于内部标头保护功能(或对于VS #pragma:on标头文件中的内容),标头的内容将不被处理,因此不会对性能造成任何影响.
There will not be any performance hit as the contents of the header will not be processed due to internal header guard (or for VS the #pragma:once in the header file.
这篇关于是否应该在项目范围的预编译头文件中重新包含已包含的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!