11

The title says it all. Have #pragma once been standardized for C++0x? I don't know any compiler that doesn't provide an implementation of it, with almost always the same semantics and name.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • 1
    See this for a [gotcha in `#pragma once`](http://stackoverflow.com/questions/787533/is-pragma-once-a-safe-include-guard/1946730#1946730) – Motti Dec 30 '10 at 19:31
  • "Almost"? You're fine using it in different compilers that don't treat it identically? Or, what other names do you know it by? (It's unclear to which one the "almost" applies, or both?) – Fred Nurk Jan 01 '11 at 10:01

3 Answers3

13

All #pragma directives cause the implementation to behave in an implementation defined way.

This hasn't changed between C++03 and the latest C++0x draft (n3225.pdf). Include guards are the portable alternative.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 1
    I know that guards are the portable alternative but I thought it could have been standardized anyway. – Klaim Dec 30 '10 at 14:51
  • Note that the latest draft is n3225.pdf. – Prasoon Saurav Dec 30 '10 at 14:55
  • 1
    @Prasoon: So it is. In my defence, the "current draft" on the WG21 web page is not up to date. http://www.open-std.org/jtc1/sc22/wg21/ – CB Bailey Dec 30 '10 at 15:01
  • Ok, `#pragma once` can cause problems, but I wonder - why not considering other alternatives, for example `#pragma once ID`, with a user-specified ID? Or am I the only to be disgusted by those copy-pasted ifdef-define-endif lines?.. – Roman L Dec 30 '10 at 20:00
  • If a kind of #pragma once become standard, that don't forces you to use it on all files, so I don't see the prolem with adding it to the standard (other than giving it a non-pragma name). – Klaim Dec 30 '10 at 21:36
  • @Klaim: Mostly just because it's completely unnecessary to add that to the language. It can already be done with the features already in the language. – Mooing Duck Apr 09 '15 at 00:42
6

Sun C++ compiler (Solaris) does not implement it. And no, it's not in C++0x drafts.

zeuxcg
  • 9,216
  • 1
  • 26
  • 33
-2

It's also trivial to implement using #ifdef. What's the guiding principal for the new version? Implement everything you might ever want and the kitchen sink or just give you the minimum tools to do so yourself?

Jay
  • 13,803
  • 4
  • 42
  • 69
  • 8
    You're missing the point. A big part of sandardisation is to make common practice be the minimal available for standard-compliant tools. As `#pragma once` IS provided by a lot of compilers and has been proved useful. A keyword equivalent standardized would have been helpful. – Klaim Dec 30 '10 at 19:06
  • 6
    include guards force you to choose names in the (one and only global) preprocessor namespace. which is a pain. granted, most projects need macros somewhere, so they have deal with the issue of choosing preprocessor identifiers without causing collisions anyway. nevertheless it's a pain, and the i still see a lot of #ifdef FILENAME_H code flying around. which is ARGH. most sensible projects use PROJECTNAME_FILENAME_SOMESUFFIX, which is OKish, but well... . IMO #pragma once is just the better solution. – Paul Groke May 23 '12 at 22:54