jacob navia said:
Richard Heathfield a écrit :
jacob navia said: [...]
#pragma once is supported by
1) MSVC (windows) [snip]
11) The Watcom compiler
Assuming your list is accurate, and removing the ones that aren't
actually used very much, leaves us with:
1) MSVC (windows)
4) gcc (versions later than 3.4) (Unix)
8) Code Warrior (MacIntosh version)
Missing from this list are a number of compilers I've used in what
we like to call The Real World:
1) earlier versions of gcc (some shops work on the basis that "if
the version we're using now does what we need, don't even *think*
about changing it");
2) Borland
3) LE370
4) C/370
5) Norcroft (used fairly heavily in the embedded world)
Excuse me but...
The reference Manual for the Norcroft compiler ARM
says in page 2-50
< quote >
2.13.3 Pragmas controlling the preprocessor
...
include_only_once
Asserts that the containing #include file is included
only once, and that if its name recurs in a subsequent
#include directive, the directive is ignored.
< end quote>
Before speaking about them you should read the docs Heathfield.
Right. So Norcroft doesn't support "#pragma once". Were you under
the impression that this supports your argument and refutes Richard's?
There are three ways to achieve the desired effect:
1. #pragma include_only_once
Supported only by Norcroft.
2. #pragma once
Supported by some, but not all compilers.
3. The usual "#ifndef" method.
Supported by every C conforming compiler.
Of course, you could write portable code that *conditionally* uses one
of the above methods, depending on which compiler you're using
(assuming that each compiler defines some unique symbol for this kind
of thing) -- but the result would be far more complex than the
*guaranteed* portable method.
And speaking of reading the documentation, section 11.3.2 of the GNU
cpp.info manual, "Obsolete once-only headers", says:
CPP supports two more ways of indicating that a header file should
be read only once. Neither one is as portable as a wrapper
`#ifndef', and we recommend you do not use them in new programs.
[snip description of "#import"]
Another way to prevent a header file from being included more than
once is with the `#pragma once' directive. If `#pragma once' is
seen when scanning a header file, that file will never be read
again, no matter what.
`#pragma once' does not have the problems that `#import' does, but
it is not recognized by all preprocessors, so you cannot rely on
it in a portable program.
This is under section 11.3, "Obsolete Features":
CPP has a number of features which are present mainly for
compatibility with older programs. We discourage their use in new
code. In some cases, we plan to remove the feature in a future
version of GCC.
This kind of thing is why we tend to discourage discussion of
compiler-specific extensions here. Even a list of dozens of compilers
that support a given feature can't compete with a statement that it's
defined by the standard, and therefore is supported by *all*
conforming compilers.