Reputation: 14454
The nonstandard #pragma once
feature is implemented on practically all C++ compilers, but the C++ standard excludes it.
The usual explanation of why #pragma once
, or some language construct that does what #pragma once
does, has been excluded from the C++ standard is that hard links and copied header files either break #pragma once
or provoke the compiler to heuristics. Fair enough, heuristics are normally incompatible with the C++ philosophy anyway, but regarding plain breakage: there are many useful language features you can break, not only #pragma once
. The normal C++ way to manage such breakage is to let the compiler issue an optional warning in doubtful instances. After all, C++ is purposely designed to let one program unsafely and/or unportably when one wishes to do so. Besides, the unsafety and/or unportability of #pragma once
is pretty minimal. It just isn't that easy to abuse.
Why is #pragma once
excluded from the standard when other abusable but useful language features are typically included? Is there something special about #pragma once
?
Also, where can one read the recent deliberations of the standards committee in the matter? Has some committee member, or committee follower, published a recent summary of the debate?
Upvotes: 3
Views: 175
Reputation: 153955
There are a few simple reasons:
#pragma once
, i.e., it isn't considered a problem.#pragma once
have a sufficient majority for it to be stopped quite quickly.There was a recent discussion on the proposals mailing list (see isocpp.org for how to sign up; I can't get to this site at the moment, though). I didn't follow it too thoroughly, though. Quickly browsing over it I saw the four reasons given above (the forth I added after browsing).
Here are some references from the recent mailing list discussion:
Upvotes: 3
Reputation: 1268
From my understanding, #pragma once
is an implementation specific instance of the standard #pragma
directive as described in Section §16.6 of the Standard (draft):
16.6 Pragma directive [cpp.pragma]
A preprocessing directive of the form
# pragma pp-tokens opt new-line causes the implementation to behave in an implementation- defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any pragma that is not recognized by the implementation is ignored.
Having pragma once
standardized would introduce quite a bit of complexity.
Give also a look here: https://stackoverflow.com/a/1696194/2741329
Upvotes: 2