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


Groups > comp.lang.c > #120711

Re: style question - includes

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: style question - includes
Date 2017-10-02 12:49 -0700
Organization None to speak of
Message-ID <ln3771s52y.fsf@kst-u.example.com> (permalink)
References (2 earlier) <7e264fae-2cac-449a-a453-432302ea79ce@googlegroups.com> <lnvajzstja.fsf@kst-u.example.com> <95591c55-890b-496e-8f94-5f5241516849@googlegroups.com> <lnfub2s2p8.fsf@kst-u.example.com> <f8f6de40-09bb-406b-bce7-a1d124a5618f@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com writes:
> On Sunday, October 1, 2017 at 9:29:46 PM UTC-5, Keith Thompson wrote:
>> supercat@casperkitty.com writes:
>> > On Saturday, September 30, 2017 at 5:36:49 PM UTC-5, Keith Thompson wrote:
>> >> Right.  N1570 6.10.2 says that #include <...> searches for a *header*,
>> >> while #include "..." searches for a *source file* (and falls back to the
>> >> <...> search).  I don't think the standard defines what a "header" is,
>> >> which means it can be anything that acts like an includable source file.
>> >
>> > As a further note, inclusion of headers is only allowed in certain contexts,
>> > while source files may be included in many other contexts.
>> 
>> There are restriction on where the language-defined standard headers can
>> meaningfully be included.  I don't think there's any such restriction on
>> where other headers can be included (though particular headers might
>> impose their own restrictions0.
>
> Does the Standard say anything about the effect of including any headers
> other than the ones listed therein?

Yes, C11 6.10.2p2:

    ... and causes the replacement of that directive by the entire
    contents of the header.

Of course the effect depends on the contents of the header.

>> >      It could also simply produce a "Headers may not be included in this
>> > context" diagnostic whether or not there would be any way to write a file
>> > that would be able to detect the context where it was included.
>> 
>> Have you seen such a diagnostic?
>
> Every system I've used has processed the <> form of #include by inserting
> text from a file stored someplace.  I find it somewhat curious that I've
> never seen system do anything else, given that that standard-header file
> processing used to represent a significant fraction of overall compilation
> time, but the Standard would certainly allow such behavior, and would
> expect that systems which process headers in a way different from files
> would have different diagnostics associated with them.

You could have just said "no".

6.10.2 says nothing about the context in which a header may be included.
It imposes some rules for the standard headers.

7.1.2p4 is a bit ambiguous:

    Standard headers may be included in any order; each may
    be included more than once in a given scope, with no effect
    different from being included only once, except that the effect
    of including <assert.h> depends on the definition of NDEBUG
    (see 7.2). If used, a header shall be included outside of
    any external declaration or definition, and it shall first be
    included before the first reference to any of the functions or
    objects it declares, or to any of the types or macros it defines.

I *think* that the second sentence is meant to apply to the standard
headers, not to headers in general (which might be supplied by the
implementation or by the programmer), but inserting the word "standard"
would improve the clarity.  For example, I think that this:

    int n =
    #include <value_of_n.h>
    ;

should be valid if the header contains nothing but the constant 42.
(Of course that's a contrived example.  It's easy to think of more
plausible examples.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Re: style question - includes jameskuyper@verizon.net - 2017-09-30 12:25 -0700
  Re: style question - includes Thiago Adams <thiago.adams@gmail.com> - 2017-09-30 13:40 -0700
  Re: style question - includes supercat@casperkitty.com - 2017-09-30 14:04 -0700
    Re: style question - includes Keith Thompson <kst-u@mib.org> - 2017-09-30 15:36 -0700
      Re: style question - includes supercat@casperkitty.com - 2017-10-01 16:07 -0700
        Re: style question - includes Keith Thompson <kst-u@mib.org> - 2017-10-01 19:28 -0700
          Re: style question - includes supercat@casperkitty.com - 2017-10-02 12:25 -0700
            Re: style question - includes Keith Thompson <kst-u@mib.org> - 2017-10-02 12:49 -0700
              Re: style question - includes "James R. Kuyper" <jameskuyper@verizon.net> - 2017-10-02 16:11 -0400

csiph-web