jacob navia said:
Tor Rustad said:
jacob navia wrote: [...]
Why not
#pragma once
Chapter and verse please.
[...]
It is supported by
1) MSVC (windows)
2) Intel compiler
2) Comeau C/C++ (Unix and windows)
3) Digital Mars C/C++ (Unix+Windows I think)
4) gcc (versions later than 3.4) (Unix)
5) lcc-win32 (Windows)
6) Delorie C/C++ (MSDOS)
7) MIPS PRO C/C++ (SGI)
8) Code Warrior (MacIntosh version)
9) Digital C++ (Unix)
10) MPW C compiler (Macintosh)
11) The Watcom compiler
The very fact that you need to list the implementations that support
it emphasizes the fact that it's non-standard.
If you happen to have the luxury of assuming that your code will be
compiled only by compilers that support '#pragma once', then it might
make sense to use it. If you intend your code to be as portable as
possible, if you don't *know* what compilers may be used to compile
your code, you need to use the old '#ifndef' trick.
For that matter, how sure can you be that all 11 of those compilers,
and all other compilers that happen to implement '#pragma once', do so
with exactly the same syntax and semantics? And do all of those
compilers accept '#pragma once' when invoked in either C90 or C99
conforming mode? If not, it could be used only at the cost of losing
checks for other non-standard constructs, even on compilers that
support it.
On some systems, there are corner cases that could cause problems.
Presumably when '#pragma once' is encountered, it causes the compiler
to remember the name of the current file and to ignore any future
#include directives for that file. But what happens if the same file
is referred to by two different names? There are a number of ways
this could happen on Unix-like systems, for example. Such problems
probably aren't too difficult to avoid, but I'd want to have a precise
definition of the feature before I'd feel comfortable using it.
(Incidentally, I just checked the gcc documentation, to see if I could
find *some* specification for '#pragma once'. It doesn't mention it
at all.)
If you want to suggest '#pragma once', that's ok with me, but *please*
don't do so without mentioning that it's non-standard. It's a nice
feature, and I wouldn't mind having it in the standard (though
defining it precisely might be difficult), but there is a cost to
using it.