为什么不是C / C ++的" #PRAGMA一次" ISO标准? [英] Why isn't C/C++'s "#pragma once" an ISO standard?

查看:100
本文介绍了为什么不是C / C ++的" #PRAGMA一次" ISO标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个大项目和维护所有这些包括警卫让我疯了!用手写它是令人沮丧的时间浪费。尽管许多编辑器可以生成包括守卫这个没有太多帮助:

I am currently working on a big project and maintaining all those include guards makes me crazy! Writing it by hand is frustrating waste of time. Although many editors can generate include guards this doesn't help much:


  1. 编辑器生成基于文件名后卫的象征。当你有在不同的目录相同的文件名头出现问题。他们都将得到同样包括后卫。包括目录结构放在护卫符号将需要编辑一些花哨的方法,因为斜杠和反斜杠宏是不是最好的事情。

  1. Editor generates guard symbol based on a filename. The problem occurs when you have headers with the same filename in different directories. Both of them will get the same include guard. Including directory structure into the guard symbol would require some fancy approach from the editor, since slashes and backslashes in the macro are not the best thing.

当我要重命名文件,我应该重新命名所有的警卫包含,以及(在IFNDEF,定义和理想ENDIF的评论)。恼人的。

When I have to rename a file I should rename all the include guards as well (in the ifndef, define and ideally endif's comment). Annoying.

preprocessor充斥吨符号没有线索,他们的意思。

Preprocessor is flooded with tons of symbols without a clue what they mean.

尽管如此定义包含一次,每次编译符合纳入头球时间还打开头。

Nevertheless definition is included once, compiler still opens header every time it meets header inclusion.

include防范不符合命名空间,也没有模板。事实上,他们正在颠覆命名空间!

Include guards don't fit into namespaces nor templates. In fact they are subverting namespaces!

您有您的后卫符号将不会是唯一的机会了。

You have a chance that your guard symbol won't be unique.

也许他们当程序包含在单独的目录中不到1000头次在可接受的解决方案。但现在?这是古老的,它无关,与现代的编码习惯。让我困扰的最多的就是这个问题可以用#pragma once指令几乎compeletly解决。为什么它不是一个标准?

Maybe they were acceptable solution in times when programs contained less than 1000 headers in single directory. But nowadays? It is ancient, it has nothing to do with modern coding habits. What bothers me the most is that this issues could be almost compeletly solved by #pragma once directive. Why is it not a standard?

推荐答案

A伪指令#pragma一次是不平凡的在有明确的完全可移植的方式来定义明确的好处。一些对于其提出的问题没有得到很好的支持 C 所有系统定义,以简单的方式定义它比传统的包括警卫可能会提供没有任何好处。这些概念

A directive like #pragma once is not trivial to define in a fully portable way that has clear an unambiguous benefits. Some of the concepts for which it raises questions are not well defined on all systems that support C, and defining it in a simple way might provide no benefit over conventional include guards.

在编译遇到的#pragma一次,应该如何识别这种文件,以便它不会再次包括它的内容?

When the compile encounters #pragma once, how should it identify this file so that it doesn't include its contents again?

答案显然是系统上的文件的独特位置。这是好的,如果系统有对所有文件不同的位置,但很多系统提供意味着一个'文件'没有一个独特的位置链接(符号链接和硬链接)。如果文件被重新列入只是因​​为它是通过一个不同的名称发现了什么?很可能不会。

The obvious answer is the unique location of the file on the system. This is fine if the system has unique locations for all files but many systems provide links (symlinks and hardlinks) that mean that a 'file' doesn't have a unique location. Should the file be re-included just because it was found via a different name? Probably not.

但现在有一个问题,怎么可能定义行为的#pragma一次在对所有平台的确切含义的方式 - 即使是那些甚至不用目录,更不用说符号链接 - 仍然得到系统的理想行为确实有他们的

But now there is a problem, how is it possible to define the behaviour of #pragma once in a way that has an exact meaning on all platforms - even those that don't even have directories, let alone symlinks - and still get the desirable behaviour on systems that do have them?

您可以说,一个文件的身份是由其内容决定的,因此,如果包含的文件有一个一度的#pragma ,并包含一个文件,该文件具有的究竟的相同的内容,那么,第二次及以后的#include s款没有任何效果。

You could say that a files identity is determined by its contents, so if an included file has a #pragma once and a file is included that has exactly the same contents, then the second and subsequent #includes shall have no effect.

这是很容易界定,并有明确定义的语义。它也有这样的良好的性质,如果项目是从支持和使用文件系统链接到一个不,它仍然表现相同的一个系统移动。

This is easy to define and has well defined semantics. It also has good properties such that if a project is moved from a system that supports and uses filesystem links to one that doesn't, it still behaves the same.

在不利的一面,每一个包含文件中包含的时间遇到​​一个的#pragma一次的内容必须与所有其他文件中使用进行检查的#pragma一旦一个已经迄今为止包括在内。这意味着性能类似于打在任何情况下使用的#include 警卫,并增加了一个不小的负担,编译器作者。显然,这样做的结果可以被高速缓存,但是同样适用于传统的包括防护装置。

On the downside, every time an include file is encountered containing a #pragma once its contents must be checked against every other file using #pragma once that has already been included so far. This implies a performance hit similar to using #include guards in any case and adds a not insignificant burden to compiler writers. Obviously, the results of this could be cached, but the same is true for conventional include guards.

常规包括警卫迫使程序员在一个宏是一个包含文件的唯一标识,但至少该行为是明确和容易实现。

Conventional include guards force the programmer to choose a macro that is the unique identifier for an include file, but at least the behaviour is well-defined and simple to implement.

由于潜在的陷阱和成本,而事实上传统的包括警卫做的工作,这并不奇怪,我认为标准委员会没有觉得有必要规范的#pragma一次

Given the potential pitfalls and costs, and the fact the conventional include guards do work, it is not surprising to me that the standards committee didn't feel the need to standardize #pragma once.

这篇关于为什么不是C / C ++的" #PRAGMA一次" ISO标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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