From: nh@cbnewsg.cb.att.com (nicholas.hounsome)
Subject: Re: Idempotent header file proposal
Organization: AT&T
Date: Fri, 20 Nov 1992 10:36:16 GMT

From article <1992Nov20.010009.24900@taumet.com>, by steve@taumet.com (Steve Clamage):
> nh@cbnewsg.cb.att.com (nicholas.hounsome) writes:
> 
>>Can we not have a standard pragma to say that a header file is idempotent
>>(A long word  I discovered that means that it only need be included once).
> 
> Not quite.  It means that (in this context) multiple inclusions have
> the same effect as one inclusion.
> 

I think that what I said means the same thing

>>eg. put #pragma IDEMPOTENT in the header and have the preprocessor
>>understand that it need not bother reading this file again it seems
>>very easy to implement provided only that you use an ANSI preprocessor
>>and it requires no language extension since it is only a convention
>>which the preprocessor can ignore if it does not understand it.
> 
> Some compilers have this feature, often called "#pragma once".
> 
> This solves a problem outside the language, and for that reason is
> probably not appropriate to put in the language Standard.  In
> particular, you are assuming that a standard header is a file, and
> that it is read and processed by the compiler in the way user text
> files are processed.  Nothing in the language specification requires
> this to be the case.  Any compiler is free to recognize the names of
> the standard headers and do any kind of magic, so long as the net
> result is what the Standard specifies.

The "standard" headers are not too much of a problem it is 
headers from large libraries such as GUIs and user headers that
make up the bulk.

> 
> For example, the compiler could have the contents of the standard
> headers already available in its symbol table but inactive when
> the compiler starts up.  It would activate the definitions
> associated with a standard header when the include directive was seen.
> This takes no time, and has no penalty if done more than once.
> 
> One problem with pragmas is that they are not necessarily harmless
> when not implemented on another compiler.  My favorite example, for
> which I cannot take credit, involves "#pragma once".  As I noted
> earlier, on some compilers it means "ignore further requests to
> include this header."  On another compiler, it might mean "turn
> on Customer Engineer features."  These features are turned off
> with "#pragma offce".
> 
> Many people involved with standards efforts believe that there should
> not be such a thing as a "standard pragma".  A pragma by its nature
> controls something non-standard, non-portable, or system-specific.
> If the thing controlled were generally portable, it should probably
> be a language feature, not a pragma.
> -- 

Point taken.

The reason that I suggested a pragma was to minimize impact on existing code.

I could have suggested changing the semantics of #include but I know that
this would break the 0.0001% of code that intentionaly includes headers
multiple times.

How about a different form for #include such as say #use with the
corresponding meaning?

> Steve Clamage, TauMetric Corp, steve@taumet.com
> Vice Chair, ANSI C++ Committee, X3J16

Others have pointed out that some compilers notice when header code is
wrapped in #ifdef HEADER1_H but this seems messy to me and I consider
readability to be a major issue.

Also for those people who say that nothing should be done because it isn't a 
language problem or can be worked around I would point out that this
applies equaly to everything that the preprocessor does. The object is
to get maintainable programs produced as fast as possible.

Nick Hounsome
AT&T Network Systems UK
Newsgroups: comp.lang.c++,comp.std.c++