Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #150250

Re: Concerning the necessity of include guards

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Concerning the necessity of include guards
Date 2020-02-25 21:18 +0100
Organization A noiseless patient Spider
Message-ID <r33vee$ro9$1@dont-email.me> (permalink)
References (11 earlier) <r33lol$c6$1@dont-email.me> <r33m2b$tlj$1@news.albasani.net> <r33mb1$423$1@dont-email.me> <r33mhq$s7u$1@news.albasani.net> <iGe5G.453980$iK5.186805@fx34.am4>

Show all headers | View raw


On 25/02/2020 20:35, Bart wrote:
> On 25/02/2020 17:46, Bonita Montero wrote:
>>> Read the Wikipedia article yourself to see why pragma once can be a 
>>> bad idea.
>>
>> That's unlikely.

Unlikely that you'll read the article?  Yes, I can agree on that.  Your 
eyes might glaze over it, but understanding will escape you.

>>
>>> Time it yourself to see if it is faster than include guards for 
>>> /your/ code with /your/ compiler.
>>
>> I don't need to time it. With include-guards the compiler would parse
>> the whole header for preprocessor-directives and there's no short-cut
>> you suggested. With #pragma once the compiler will only need to parse
>> the first line of the header.
> 
> Probably not even the first line. If it has already seen a header H with 
> its '#pragma once' the first time, and compiled that header, then if the 
> program includes H again, it will already know not to process it again.

Yes. that seems likely.

> 
> In any case, '#pragma once' need not be on the first line; it can be 
> anywhere in the file, incuding at the end. (And, ironically, can be 
> specified more than once.)
> 
> (FWIW my own C compiler ignores '#pragma once'. Although it doesn't need 
> it because parsing headers is very fast anyway, with or without guards, 
> there can be problems when programs /rely/ on '#pragma once' to avoid 
> compile errors.
> 
> So I may add it when I've got some time.)

It is probably a good idea to throw an error on it if you don't 
implement it in a way that people expect.  You have the choice (in the 
sense of "the C standard says") of ignoring such unrecognised or 
unimplemented pragmas, or claiming you recognize them and they have the 
implementation-defined behaviour of halting with an error.

> 
>  > I don't need to time it. With include-guards the compiler would parse
>  > the whole header for preprocessor-directives and there's no short-cut
> 
> The way I do it, false blocks of #if/#ifdef do not need normal parsing; 
> tokenising is sufficient. But even then, it can use streamlined 
> tokenising as it is only interested in the next #-directive to see if it 
> is a matching #else, #elif or #endif.

Exactly.  This will be done very quickly by most serious compilers. 
Even gcc :-)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Concerning the necessity of include guards Anton Shepelev <anton.txt@gmail.com> - 2020-02-24 22:08 +0300
  Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-24 20:28 +0100
  Re: Concerning the necessity of include guards Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-24 19:40 +0000
    Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-24 22:37 +0100
      Re: Concerning the necessity of include guards Anton Shepelev <anton.txt@g{oogle}mail.com> - 2020-02-25 14:13 +0300
        Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 13:03 +0100
          Re: Concerning the necessity of include guards Anton Shepelev <anton.txt@gmail.com> - 2020-02-29 01:14 +0300
            Re: Concerning the necessity of include guards Bart <bc@freeuk.com> - 2020-02-28 22:36 +0000
              Re: Concerning the necessity of include guards Anton Shepelev <anton.txt@gmail.com> - 2020-02-29 13:28 +0300
            Re: Concerning the necessity of include guards Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-02-29 00:08 +0000
            Re: Concerning the necessity of include guards Richard Damon <Richard@Damon-Family.org> - 2020-02-29 07:05 -0500
            Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-03-01 15:01 +0100
  Re: Concerning the necessity of include guards Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-02-24 14:52 -0500
    Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-24 21:19 +0100
      Re: Concerning the necessity of include guards Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-02-24 15:48 -0500
        Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-24 21:53 +0100
          Re: Concerning the necessity of include guards Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-02-24 17:26 -0500
            Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 00:08 +0100
              Re: Concerning the necessity of include guards Thiago Adams <thiago.adams@gmail.com> - 2020-02-24 15:24 -0800
                Re: Concerning the necessity of include guards Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-24 22:20 -0800
                Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 09:52 +0100
            Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-25 08:12 +0100
              Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 09:55 +0100
                Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-25 10:35 +0100
                Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 13:13 +0100
                Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-25 13:50 +0100
                Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 18:33 +0100
                Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-25 18:38 +0100
                Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 18:42 +0100
                Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-25 18:46 +0100
                Re: Concerning the necessity of include guards Bart <bc@freeuk.com> - 2020-02-25 19:35 +0000
                Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 21:18 +0100
                Re: Concerning the necessity of include guards Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-26 06:40 +0100
                Re: Concerning the necessity of include guards James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-25 12:12 -0800
                Re: Concerning the necessity of include guards James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-25 11:55 -0800
                Re: Concerning the necessity of include guards David Brown <david.brown@hesbynett.no> - 2020-02-25 21:37 +0100
                Re: Concerning the necessity of include guards scott@slp53.sl.home (Scott Lurndal) - 2020-02-26 02:39 +0000
                Re: Concerning the necessity of include guards Real Troll <real.troll@trolls.com> - 2020-02-25 23:10 -0400
                Re: Concerning the necessity of include guards chad <cdalten@gmail.com> - 2020-02-27 05:39 -0800
  Re: Concerning the necessity of include guards Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-02-24 22:23 +0000
  Re: Concerning the necessity of include guards Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-24 21:17 -0800
    Re: Concerning the necessity of include guards Guillaume <message@bottle.org> - 2020-02-25 13:03 +0100

csiph-web