#pragma once vs 包含守卫? [英] #pragma once vs include guards?

查看:25
本文介绍了#pragma once vs 包含守卫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个已知只能在 Windows 上运行并在 Visual Studio 下编译的代码库(它与 excel 紧密集成,因此它不会去任何地方).我想知道我是否应该使用传统的包含守卫或使用 #pragma once 作为我们的代码.我认为让编译器处理 #pragma once 会产生更快的编译,并且在复制和粘贴时更不容易出错.它也稍微不那么丑;)

I'm working on a codebase that is known to only run on windows and be compiled under Visual Studio (it integrates tightly with excel so it's not going anywhere). I'm wondering if I should go with the traditional include guards or use #pragma once for our code. I would think letting the compiler deal with #pragma once will yield faster compiles and is less error prone when copying and pasting. It is also slightly less ugly ;)

注意:为了获得更快的编译时间,我们可以使用 Redundant Include Guards 但那在包含文件和包含文件之间添加紧密耦合.通常没问题,因为保护应该基于文件名,并且只有在您无论如何需要更改包含名称时才会更改.

Note: to get the faster compile times we could use Redundant Include Guards but that adds a tight coupling between the included file and the including file. Usually it's ok because the guard should be based on the file name and would only change if you needed to change in the include name anyways.

推荐答案

我认为它不会对编译时间产生重大影响,但是 #pragma once 在编译器之间得到很好的支持,但不是实际上是标准的一部分.预处理器可能会更快一些,因为它更容易理解您的确切意图.

I don't think it will make a significant difference in compile time but #pragma once is very well supported across compilers but not actually part of the standard. The preprocessor may be a little faster with it as it is more simple to understand your exact intent.

#pragma once 不太容易出错,输入的代码也更少.

#pragma once is less prone to making mistakes and it is less code to type.

要加快编译时间,只需向前声明,而不是在可能的情况下将其包含在 .h 文件中.

To speed up compile time more just forward declare instead of including in .h files when you can.

我更喜欢使用#pragma once.

请参阅这篇维基百科文章,了解两者都使用的可能性.

这篇关于#pragma once vs 包含守卫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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