Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!mit-eddie!uw-beaver!uw-june!pardo From: pardo@cs.washington.edu (David Keppel) Newsgroups: gnu.gcc Subject: Re: #pragma once Message-ID: <9754@june.cs.washington.edu> Date: 9 Nov 89 17:37:30 GMT References: <8910300524.AA11104@sugar-bombs.ai.mit.edu> <1989Nov1.094352.24529@paris.ics.uci.edu> <522@loft386.UUCP> <305@jhereg.Minnetech.MN.ORG> Reply-To: pardo@june.cs.washington.edu (David Keppel) Distribution: gnu Organization: University of Washington, Computer Science, Seattle Lines: 50 mark@jhereg.UUCP (Mark H. Colburn) writes: >[Why `#pragma once'? What's wrong with > #ifndef TOKEN > #define TOKEN > ... > #endif TOKEN >] It's slow, particularly if you have nested includes. The problem is that with `#ifndef TOKEN ... #endif' the entire file must be scanned each place that the #include directive appears, even if the #include is redundent. With `#pragma once', the compiler can avoid redundent rescans. An portable alternative convention avoids the overhead of rescanning but requires more programmer work and allows more typos: #ifndef TOKEN # include TOKEN #endif I ususually combine the latter with lines in the .h that look like #ifdef TOKEN { TOKEN multiply defined! } #else #define TOKEN ... #endif Alternatively, you can just do `#ifndef TOKEN ... #endif' and let the compile go slow if you (somebody) does some repeated includes. To summarize: `#pragma once' provides a facility that is available by other mechanisms. The advantage of `#pragma once' is that information about the use of the .h is not spread through both the .h and the .c. Using `#pragma once' does not compromise portability if the `#prgama once' is ifdeffed. #ifdef __STDC__ #pragma once #endif Note that the octothorpe ('#') should be indented to keep older compilers from gagging. ;-D on ( #pragma once upon a time ) Pardo -- pardo@cs.washington.edu {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo