
Signature
Metrowerks Community Forum is a free online resource for developers
to discuss CodeWarrior topics with other users and Metrowerks' staff
-- http://www.metrowerks.com/community --
Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com
> #pragma once may not be portable so below is better... but....
Agreed, but CW, VC and GCC support it, so for my simple closed source,
Mac OS X (and perhaps later Win32) application needs it will do fine.
I saw this in a source file I found online:
#ifndef ACE_ARG_SHIFTER_H
#define ACE_ARG_SHIFTER_H
#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */
/* ... all header stuff ... */
#endif /* ACE_ARG_SHIFTER_H */
So, they wrap the entire header in an #ifndef XXX AND then, if they have
statically determined that #pragma once is available in the compiler,
they add that too! Am I missing something or is this just really
redundant? I can understand the use of a #define to directly test for an
included header, like Apple does in most of it's headers, but doing both
seems a bit much? MSVC also does this when it generates a header and
I've seen this construct quite frequently elsewhere, but I cannot see
the use of this. Can somebody explain?
>>#define __MY_HEADER_H__
>
[quoted text clipped - 6 lines]
> The mistake comes in because people look at headers like MSL and see
> that style being used and adapt it.
Ah, thanks for the heads up. I never ran into problems with this since I
don't really use external libraries, but I'll modify it if I still find
one in my sources (I always just use #pragma once now.)
Arthur
David Phillip Oster - 23 Dec 2004 17:55 GMT
In article <41ca9816$0$144$e4fe514c@news.xs4all.nl>,
Arthur Langereis <arthurREPLACEWITHUNDERSCOREpublic@xfinitegames.com>
wrote:
> > #pragma once may not be portable so below is better... but....
>
[quoted text clipped - 22 lines]
> I've seen this construct quite frequently elsewhere, but I cannot see
> the use of this. Can somebody explain?
If all you've got is #ifndef guards, then the compiler has do file i/o
to fetch the header file, and it has to scan the newly-read-in .h file
looking for the closing #endif. With #pragma once, or #import, all that
work can be skipped for a redundant include.

Signature
David Phillip Oster
Miro Jurisic - 23 Dec 2004 19:23 GMT
> If all you've got is #ifndef guards, then the compiler has do file i/o
> to fetch the header file, and it has to scan the newly-read-in .h file
> looking for the closing #endif. With #pragma once, or #import, all that
> work can be skipped for a redundant include.
Actually, all that information can be cached and this is, AFAIK, a common
optimization in modern compilers. (This also eliminates benefits of using the
#ifndef/#include/#endif idiom.)
meeroh

Signature
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>
Eric Albert - 23 Dec 2004 17:57 GMT
In article <41ca9816$0$144$e4fe514c@news.xs4all.nl>,
Arthur Langereis <arthurREPLACEWITHUNDERSCOREpublic@xfinitegames.com>
wrote:
> > #pragma once may not be portable so below is better... but....
>
[quoted text clipped - 18 lines]
> they add that too! Am I missing something or is this just really
> redundant?
Yeah, it's completely redundant. It'd only be required if a compiler
didn't implement #ifndef/#define/#endif correctly and did implement
#pragma once, but any compiler that did so wouldn't be compliant with
one of the most simple aspects of ANSI C (to say nothing of subsequent C
standards) and nobody would use it.
The only other reason to do this is that a compiler might optimize the
#pragma once case when including header files to know that it should
check for duplication of the header file now and avoid preprocessing the
rest of the file a second time. But any good compiler should do the
same optimization for a header file completely surrounded by
#ifndef/#define/#endif anyway, and the need for that optimization is
largely eliminated by precompiled headers anyway (which are now
supported by all of CodeWarrior, Visual C++ and GCC).
-Eric

Signature
Eric Albert ejalbert@cs.stanford.edu
http://outofcheese.org/