Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!labrea!rutgers!rochester!pt.cs.cmu.edu!pt!dld From: dld@F.GP.CS.CMU.EDU (David Detlefs) Newsgroups: comp.lang.c++ Subject: Re: Recursive #includes Message-ID: Date: 27 Feb 89 16:59:37 GMT Organization: CMU CS Department Lines: 43 Recently, Paul Chisholm replied to Dominich Samperi's query on this subject, saying that this idea had been beaten to death recently. He went on to summarize the discussion, saying roughly that the two extreme solutions to "the include file problem" are 1) Write "foo.h" in the form #ifndef _FOO_H_ #define _FOO_H_ 1 #endif // _FOO_H_ This requires no modifications to cpp. 2) Build a cpp so that it never includes a file twice -- this increases efficeincy significantly because, unlike the above, it doesn't even have to open the file to determine that it doesn't need to be included. Unfortunately, despite what many of us might think about such usage, some files are written to be included multiple times. Many people in the discussion tried to find compromise solutions; I was one of them. Unfortunately, my idea didn't work. I'm posting to plug what I thought was the best solution I read; I think all cpp's should be written this way (I forget who proposed this -- they should take a bow, I think): When cpp first processes a file, it determines all non-comment, non-whitespace text in the file is enclosed in an #ifndef block, as above. If so, the file name and the variable whose definedness determines inclusion are put in a table. On subsequent includes, the file is opened and processed only if the variable is not defined. This would seem to have all the performance advantages of proposal 2, while retaining all the compatibility advantages of proposal 1. Just a marvelous idea. -- 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.) --