#pragma是C ++ 11标准的一部分吗? [英] Is #pragma once part of the C++11 standard?

查看:222
本文介绍了#pragma是C ++ 11标准的一部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传统上,在C ++中避免多个头包含的标准和可移植方法是/是使用 #ifndef - #define - #endif 预编译指令称为宏观防护计划(请参阅下面的代码段)。

Traditionally, the standard and portable way to avoid multiple header inclusions in C++ was/is to use the #ifndef - #define - #endifpre-compiler directives scheme also called macro-guard scheme (see code snippet below).

#ifndef MY_HEADER_HPP
#define MY_HEADER_HPP
...
#endif

然而,在大多数实现/编译器(见下图)中有一个更优雅的替代方案,其作用与宏指令方案#pragma once #pragma once 与宏保护方案相比有几个优点,包括更少的代码,避免名称冲突,有时提高了编译速度。

In most implementations/compilers (see picture below) however, there's a more "elegant" alternative that serves the same purpose as the macro-guard scheme called #pragma once. #pragma once has several advantages compared to the macro-guard scheme, including less code, avoidance of name clashes, and sometimes improved compile speed.

>

做一些研究,我意识到虽然 #pragma once 指令由几乎所有已知的编译器支持,但是有一个混乱是否 #pragma once 指令是C ++ 11标准的一部分。

Doing some research, I realized that although #pragma once directive is supported by almost all known compilers, there's a turbidness on whether #pragma once directive is part of the C++11 standard or not.


  • 有人可以澄清 #pragma once 指令是否是C ++ 11标准的一部分? li>
  • 如果它不是C ++ 11标准的一部分,是否有计划将它包含在以后的版本中(例如,C ++ 14或更高版本)?

  • 如果有人可以进一步详细说明使用其中一种技术(即宏 - 保护与 #pragma once )的优点/缺点,这也将是很好的。

  • Could someone clarify whether #pragma once directive is part of the C++11 standard or not?
  • If it's not part of the C++11 standard, are there any plans on including it on later releases (e.g., C++14 or later)?
  • It would also be nice if someone could further elaborate on the advantages/disadvantages in using either one of the techniques (i.e., macro-guard versus #pragma once).

推荐答案

#pragma once 标准。它是一种广泛的(但不是
通用的)扩展,如果您的可移植性问题有限,可以使用

#pragma once is not standard. It is a widespread (but not universal) extension, which can be used


  • / li>
  • 您可以确定所有的包含档案总是在本机磁碟上。

它被认为用于标准化,但被拒绝,因为
不能可靠地实现。 (当你
有文件可以通过几个不同的远程装载访问文件时出现问题。)

It was considered for standardization, but rejected because it cannot be implemented reliably. (The problems occur when you have files accessible through several different remote mounts.)

这很容易确保没有包含警卫
冲突在一个单一的发展。对于库,可能
被许多不同的发展使用,显而易见的解决方案是
生成大量的随机字符包括警卫
当你创建它。 (一个好的编辑器可以设置为
,当你打开一个新的标题。)但是即使没有这个,
我还没有遇到任何问题之间的冲突
库。

It's fairly easy to ensure that there are no include guard conflicts within a single development. For libraries, which may be used by many different developments, the obvious solution is to generate a lot of random characters for the include guard when you create it. (A good editor can be set up to do this for you whenever you open a new header.) But even without this, I've yet to encounter any problems with conflicts between libraries.

这篇关于#pragma是C ++ 11标准的一部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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