Path: utzoo!utgpu!watmath!clyde!att!rutgers!mit-eddie!bloom-beacon!apple!darin
From: darin@Apple.COM (Darin Adler)
Newsgroups: comp.lang.c++
Subject: Re: Including header files minimally.
Message-ID: <21780@apple.Apple.COM>
Date: 6 Dec 88 21:21:15 GMT
References: <3561@pt.cs.cmu.edu> <7860@nsc.nsc.com> <3614@pt.cs.cmu.edu> <10873@ulysses.homer.nj.att.com> <1073@actnyc.UUCP> <738@quintus.UUCP> <1988Nov25.180309.9323@utzoo.uucp> <8080@nsc.nsc.com> <562@redsox.UUCP> <1988Nov29.203751.26424@utzoo.uucp>
Organization: Apple
Lines: 40

In article <1988Nov29.203751.26424@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
> In article <562@redsox.UUCP> campbell@redsox.UUCP (Larry Campbell) writes:
> >How portable can something be that's not even implemented yet???  If you
> >just wrap your header files [in #ifndef]...
> >it achieves the result you desire, completely portably, today, in both
> >C++ and C, without requiring changes to preprocessors or funky #pragma
> >statements... Really, I think this is a SOLVED PROBLEM ...
> 
> Not so.  The problem is that a header wrapped in #ifndef still needs to
> be opened, read, and scanned completely every time, which is costly when
> the #include relationships are complex and common header files get picked
> up many, many times.  One can imagine a tricky compiler which notices
> the wrapping and optimizes this case, but that's not going to be easy.

I have to disagree here, Henry. Writing a compiler (or preprocessor) that
notices the wrapping and optimizes it is extremely easy. While the preprocessor
is scanning a file it simply sets a "start of file" flag which is cleared
when anything besides a comment is seen. If an #ifndef occurs with this flag
set, the preprocessor symbol can be recorded a table, along with the file name.
(Of course, another check to be sure that the #endif is the last non-comment
line in the file as well would be necessary, but that is left as a simple
exercise for the implementor.) Then, any time the same file is included,
that symbol can be checked...if it is defined, the file need not be opened.
Note that even if the system can't recognize "this.h" and  and
"/this.h" as the same file, there is no problem, since the file protects itself
from multiple inclusion.

> In practice, a header file using "#pragma idempotent" or whatever would
> still include the #ifndef wrapper, for the sake of portability.  (There
> is no requirement that #pragma be portable, but there *is* a requirement
> that ANSI C implementations ignore unrecognized #pragmas.)

Note that the above scheme is superior because it gets the same results as
the pragma without typing it, since you still plan to include the #ifndef
wrapper. I'm ready to recommend to our compiler folks that they implement
the scheme now!
--
Darin Adler					              AppleLink: Adler4
UUCP: {amdcad,decwrl,hoptoad,nsc,sun}!apple!darin     Internet: darin@Apple.com