Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!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: Is "#pragma once" necessary? Message-ID: Date: 27 Jul 89 23:24:35 GMT References: <117933@sun.Eng.Sun.COM> Organization: CMU CS Department Lines: 36 In-reply-to: plocher%sally@Sun.COM's message of 26 Jul 89 22:46:09 GMT Because I really admire whoever thought of this idea, I'll continue to try to explain it better: John Plocher (plocher@sun.Eng.Sun.Com?) writes >But what if I don't write it in the magic form you describe? Then your >cute trick breaks. > >You seem to use FOO_H as the token used for the #ifndef - I use _FOO_H_. >curses.h may well use #ifndef mvwputc, and serialio.h may use #ifndef IOGETBAUDRATE I'm sorry: in my example, I intended FOO_H to stand for any preprocessor variable name at all, not literally "FOO_H." I thought this would be clear. The first time through, you associate the variable name with the file name; on subsequent inclusios, you see if the variable is defined before opening the file. >then how can you distinguish between this and something that happens to have a #ifndef >at the beginning which is NOT intended to be used as a Only One Inclusion flag? The point is that the intention doesn't matter; if a file is of this form, then it only needs to be included once (if the #ifndef variable is defined at the time of later inclusions): subsequent inclusions will result in no non-comment text. Which is exactly why #ifndef is used this way. You are right that "#pragma once" says the same thing more succintly; my objections are (again) 1) that a "pragma" is usually taken to mean something that affects performance and not semantics, while pragma once may definitely affect semantics, and 2) that in the absence of really good reasons such as performance, which my proposal solves, we shouldn't arbitrarily abandon existing mechanisms. -- 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.)