Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!pt.cs.cmu.edu!pt!dld From: dld@F.GP.CS.CMU.EDU (David Detlefs) Newsgroups: gnu.gcc Subject: Re: #pragma once Message-ID: Date: 30 Oct 89 19:52:05 GMT References: <8910300524.AA11104@sugar-bombs.ai.mit.edu> Distribution: gnu Organization: CMU CS Department Lines: 71 In-reply-to: rms@AI.MIT.EDU's message of 30 Oct 89 05:24:27 GMT (This may be better placed in bug-gcc, but I don't normally read that.) #pragma once has bothered me for a while. I recently corresponded with Ron Guillemette (sp? sorry) on the issue, and the discussion caused me to clarify the reasons for my uneasiness. 1) Reading Harbison and Steele's description of Draft Proposed ANSI C, p. 254, we see that "... implementations should ignore [pragma] information they do not understand." Thus, an ANSI C compiler may validly include a .h file containing #pragma once multiple times. 2) rms recently posted that >If you want to make sure a file won't be reincluded, you should use >`#pragma once' together with suitable conditionals that would prevent >multiple processing of the header file's contents. You can't rely on >`#pragma once'--it is just a time-saving hint. What happens if a file contains a #pragma once but does not contain conditionals suitable to prevent it's contents from being processed multiple times? It certainly seems to me that if the file is not processed on inclusions after the first, then the (Draft Proposed) ANSI standard has been violated. A C implementation that uses the "#pragma" once in this way produces semantically different results than one that ignores it; in effect, this interpretation makes it not a "pragma," in the sense that this term is usually used (non-semantics affecting.) 3) There is no way that I can see to make "#pragma once" safe; that is, a pragma in the sense defined in the ANSI C proposal. The only way to do so is to have cpp do enough analysis to determine whether the #pragma once file contains "suitable conditionals" (to use rms's term) to prevent subsequent reinclusion. Even then, an including file might #undef the variable whose definedness prevents processing. If this is not recognized, semantics are not preserved. The best idea I have ever heard along these lines was recently reiterated by John Nagle on comp.lang.c++ (I don't know if it's original with him): have cpp recognize files of the common form : #ifndef #endif maintain a table associating filenames with variables . whenever we encounter #include "" check to see if is in the table, and if so, if the corresponding is currently defined. If so, we may safely pass over the #include. Note that this gets all the performance savings of #pragma once, while maintaining ANSI semantics. If you read rms's post again, you'll note that you are supposed to use *both* #pragma once and the #ifndef style. This proposal obviates the need for the #pragma once. This is not my idea, but I really think it's a good one. Hope I've convinced somebody. -- 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.)