Path: utzoo!utgpu!watmath!watdragon!akwright From: akwright@watdragon.waterloo.edu (Andrew K. Wright) Newsgroups: comp.lang.c++ Subject: Re: Including header files minimally. Message-ID: <10036@watdragon.waterloo.edu> Date: 28 Nov 88 16:30:16 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> Reply-To: akwright@watdragon.waterloo.edu (Andrew K. Wright) Organization: U. of Waterloo, Ontario Lines: 68 In article <8080@nsc.nsc.com> rfg@nsc.nsc.com.UUCP (Ron Guilmette) writes: >In article <1988Nov25.180309.9323@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes: >>In most cases, the fact >>that #including a file n times is the same as including it once is a >>property of the file being included, not the file doing the including. >> >>The probability of trouble and mistakes will be much lower if it's the >>file being included that determines whether future inclusions have any >>effect. > >Exactly right. > >>The Waterloo "#pragma idempotent" strikes me as the right method; >>among other things, it means that your code is portable. > >Is this (semantically) the same thing as I am suggesting? I have never >heard of this before! If it has the effect of forcing the file it is >found within to only be included once then I guess that there is at least >one clear (although not yet standard) precedent for an approach to this >problem. I will expand a bit on my original posting about #pragma idempotent (or #pragma once as Ron Guilmette suggests). When a file marked with #pragma idempotent is included the first time, the preprocessor makes a note of this. If the file is later included again, the include directive is simply ignored. Determining when the file is included "again" means determining when two include paths are equal. You might want to go by physical file equality (ie. compare devices and i-nodes), pathname equality, or string equality. The Waterloo compiler uses simple string equality: two include paths are considered equal if strcmp() on the argument strings of the include directives, modulo their quotes or <>s, reports they are equal. We think it is a bad idea to build operating system dependant features such as links or i-nodes into the semantics of the language; C runs on many non-UNIX systems without such features. Besides, you will never get the ANSI committee to agree to such. Another poster noted that idempotency is not entirely an attribute of the included file. This is true; both the includer and the includee must agree that the file is to have idempotent semantics, or someone will get surprised. Thus #pragma idempotent is more than just a statement that redundant #includes of this file will be ignored; it asserts that the user is NOT ALLOWED to redefine any names appearing in such a file. This matches the ANSI committee's intent for the standard include files: the user is not permitted to redefine anything infor instance. (He is permitted to #undef macros to get real functions). can therefore be marked with #pragma idempotent, as can all the other standard include files. In fact, It just occurs to me that CPP could mark identifiers brought in by idempotent include files as non-redefinable. This has the obvious advantage that the ANSI rule " standard identifiers cannot be redefined" becomes a compiler enforced rule, not just a vapor rule. (Personally I hate the idea of several hundred reserved words, but the committee seems to be well on its way down that path.) Lastly, #pragma idempotent is upwards compatible. It does not break *any* (not just few) existing C programs. It does have the disadvantage that pragmas are not required (by ANSI) to be supported by all compilers, so you may still have to wrap #ifndefs around your include file bodies if you expect to have your code ported to any C compiler. Andrew K. Wright akwright@watmath.waterloo.edu CS Dept., University of Waterloo, Ont., Canada.