何时使用包含警卫或#pragma一次C ++ [英] When to use include guards or #pragma once C++

查看:145
本文介绍了何时使用包含警卫或#pragma一次C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个头文件中使用您选择的/两者包括警卫和 #pragma一次,或仅使用诸如类声明之类的东西?

Is it good practice to use your choice of either/both include guards and #pragma once in every header file, or just those with something such as a class declaration?

我很想把它放在每个标题中,但我担心它会被删除,只会增加编译时间。什么是好的做法或常见的做法?

I am tempted to put it in every header, but I'm afraid it would be unneeded and only add to the compile time. What is good practice or common to do?

让我澄清一下:我理解两者之间的区别。我问经验丰富的程序员是否在每个文件中使用它,或者只是那些需要它的人。

Let me clarify: I understand the difference between the two. I am asking whether by experience programmers use it in every file or just those that require it.

推荐答案

总结Galik的评论以及我意识到的事情:

Summarizing the comment by Galik and what I realized:

如果未来出现问题,应该在每个头文件中加入警戒冲突。此外,编译器处理包含保护所需的时间很短,因为不需要处理额外的标头,所以编译速度会更快。

Include guards should be put in every header file in the case that something in the future conflicts. Furthermore, the small time it takes the compiler to process the include guards will make the compilation faster since the extra header does not need to be processed.

这篇关于何时使用包含警卫或#pragma一次C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆