Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!pt.cs.cmu.edu!pt!dld From: dld@F.GP.CS.CMU.EDU (David Detlefs) Newsgroups: comp.lang.c++ Subject: Re: A solution to the multiple inclusion problem Message-ID: Date: 25 Oct 89 14:41:01 GMT Organization: CMU CS Department Lines: 68 There have been a lot of posts on this subject ever since John Nagle made the original post. I believe that most of them miss the point. The various objections that have been made, and responses to them: 1) It's bad practice to have non-idempotent include files. Answer) Sure, I think I agree, but there are programs that use them. 2) The Ansi standard will mandate single-inclusion semantics (John Hascall.) Answer) As has been pointed out (Tom Neff, Michael Meissner), only for explicitly specified library include files. Others must obey current semantics. 3) You could a) move the #ifndef/#endif into the *including* file (Lawrence Crowl, John Ciccarelli, Peter da Silva), or b) add a new "#include-once" or "#pragma once" (Scott Schwartz, Blair Houghton, Jonathon Shapiro) directive to the preprocessor and use that to get the same effect. Answer) Sure you could, but Nagle's proposal gets you optimal performance with *no* semantic changes, and *no* extra programmer work. 4) You could write your include files in 2 parts, a "wrapper" that only includes the "real" file once. (Michael J. Zehr, MIT, Rich Salz) Answer) I believe you will find that a significant proportion of the cost of multiple includes is embodied in the disk seeks necessary to find the file just to open it. This would imply that this scheme will not save as much as you'd like. But this is a guess on my part, subject to empirical test. Does the proponent of the wrapper scheme care to run the comparison? In any case, observe that Nagle's scheme performs *optimally* with no extra programmer effort of this kind. 5) The performance difference may not be worth worrying about (Ken Yap.) Answer) Often true, but in the cases where it's not true, it can be very not true (My own experience, John Ciccarelli). **** The point of Nagle's suggestion is that 1) There is an existing very often used method for getting include-once *semantics*, viz., writing include files in the form #ifndef #define 1 #endif 2) He presents a technique that improves performance without modifying semantics in *any way.* (Do you know how rare such an idea is!?!) Conclusion: IMHO, anybody who's read Nagle's post and implements a C preprocessor (or compiler that incorporates one) and doesn't use the technique doesn't recognize a good thing when it walks up and sits in his/her lap. Even if your CPP incorporates a mechanism such as #pragma once, this will still help if the compiler is used on any of the vast existing body of code that doesn't use #pragma once. Dave -- Dave Detlefs Any correlation between my employer's opinion Carnegie-Mellon CS and my own is statistical rather than causal, dld@cs.cmu.edu except in those cases where I have helped to form my employer's opinion. (Null disclaimer.)