MSDN中关于#pragma once的译文

同样对于#pragma once的解释,MSDN的中文翻译少了很多知识点,所以我就将英文中对应的这些知识点简单的翻译一下,同时还有一些疑惑。

pragma once


Remark


  • [1]

The use of #pragma once can reduce build times as the compiler will not open and read the file after the first #include of the file in the translation unit. This is referred to as multiple-include optimization. It has an effect similar to the #include guard idiom, which uses preprocessor macro definitions to prevent multiple inclusion of the contents of the file. This also helps to prevent violations of the one definition rule—the requirement that all templates, types, functions, and objects have no more than one definition in your code.
译文#pragma once的使用可以减少编译次数,因为当一个编译单元包含一次这个头文件后,该编译单元就不再对pragma once进行读取(open and read)了。#pragma once对multiple-include(我不知道怎么将它翻译成汉语术语)这个问题进行了优化。#pragma once#include guard idiom(注:#inlcude guard idiom 就是#ifnedf... #define... #endif..)的作用是类似的;#include guard idiom是通过预处理的宏定义去抑制文件中的内容被多次包含,也就是说如果我在一个编译单元中使用了#include guard idiom,那么它也只被包含一次(prevent multiple inclusion of the contents of the file);#pragma once也可以对“one definition rule”进行保护(“one definition rule”即是对于任何模板、类型、函数、对象在你的代码中仅仅只能被包含一次);

这里我们就可以举个栗子;

//header.h
#pragma once
//Code placed here is inlcuded only once per translation unit; 

注:对于#pragma once的使用,它仅仅是在每个编译单元中被包含一次;


  • [2]

We recommend the #pragma once directive for new code because it doesn’t pollute the global namespace with a preprocessor symbol. It requires less typing, is less distracting, and can’t cause symbol collisions—errors caused when different header files use the same preprocessor symbol as the guard value. It is not part of the C++ Standard, but it is implemented portably by several common compilers
译文:对于新写的代码,我们推荐使用#pragma once这个术语,它不会在全局空间中造成宏定义的泛滥;它不必让人绞尽脑汁写不同的宏,也不会产生干扰,当然也就不会导致符号(宏)冲突;当不同的头文件尝试使用相同的预处理宏时就会产生上述的符号冲突;#pragma once不是C++的标准,但是它已经被多个编译器实现,可以在这些编译器上进行移植;


  • [3]

There is no advantage to use of both the #include guard idiom and #pragma once in the same file. The compiler recognizes the #include guard idiom and implements the multiple include optimization the same way as the #pragma once directive if no non-comment code or preprocessor directive comes before or after the standard form of the idiom:
译文:在一个头文件中同时使用#include guard idiom和#pragma once并不能带来显著的性能提升;一旦编译器识别了#include guard idiom之后,它实现multiple include优化和#pragma once是相同的;前提是非内容代码或者预处理的宏与这个#include guard idiom不同(这部分我感觉自己翻译的太烂,所以大家意会吧==);

举个include的栗子:

// header.h
// #include guard idiom的用例展示。
// 这里定义的宏名是随意的,可以是HEADER_H_也可以是其他的。
#ifndef HEADER_H_     /
// Code placed here is included only once per translation unit
#endif // HEADER_H_

这里需要注意的同样是如果使用了#include guard idiom那么在一个编译单元中仅仅就需要include一次就行;


  • [4]

We recommend the #include guard idiom when code must be portable to compilers that do not implement the #pragma once directive, to maintain consistency with existing code, or when the multiple-include optimization is impossible. This can occur in complex projects when file system aliasing or aliased include paths prevent the compiler from identifying identical include files by canonical path
译文:当编译器不支持#pragma once这个指令时,我们推荐使用#include guard idiom。这样可以保持原有代码的连续性,毕竟#include是由语言进行支持的;还有一种情况就是当multiple-include没法实现的时候,我们推荐使用#include guard idiom。


  • [5]

Be careful not to use #pragma once or the #include guard idiom in header files that are designed to be included multiple times, using preprocessor symbols to control their effects. For an example of this design, see the <assert.h> header file. (这句没有看懂,所以暂时留个坑放在这里,大神帮我翻译一下也好^_^)
Also be careful to manage include paths to avoid creating multiple paths to included files, which can defeat the multiple-include optimization for both #include guards and #pragma once.
译文:对#pragma once#include guard idiom的使用还有一个小心的地方就是对于文件路径的管理一定要小心,不要造成include多条路径时,这些路径指向的确实同一个文件,这种情况下#pragma once#include guard idiom对multiple-include的优化可能失效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值