The order of header inclusion

Discussion in 'C++' started by Pavel, Jul 8, 2009.

  1. Pavel

    Pavel Guest

    I vaguely remembered reading something in the Standard while ago that
    somehow discouraged the inclusion of user headers before Standard C++
    Library headers. I tried to refresh my memory and looked up in the
    Standard but could not find anything. Does anybody know any such
    restriction/recommendation or has it never existed?

    Thanks,
    -Pavel
     
    Pavel, Jul 8, 2009
    #1
    1. Advertising

  2. * Pavel:
    > I vaguely remembered reading something in the Standard while ago that
    > somehow discouraged the inclusion of user headers before Standard C++
    > Library headers. I tried to refresh my memory and looked up in the
    > Standard but could not find anything. Does anybody know any such
    > restriction/recommendation


    The usual recommandation is the opposite: to never include standard headers
    before user headers.

    The rationale is that one should to try to minimize the cases where something
    works by accident.

    Or at least, not let something work by accident when it's easy to avoid.


    > or has it never existed?


    No.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Jul 8, 2009
    #2
    1. Advertising

  3. Pavel

    Fred Zwarts Guest

    "Pavel" <dot_com_yahoo@paultolk_reverse.yourself> wrote in message news:k8U4m.2852$...
    >I vaguely remembered reading something in the Standard while ago that
    > somehow discouraged the inclusion of user headers before Standard C++
    > Library headers. I tried to refresh my memory and looked up in the
    > Standard but could not find anything. Does anybody know any such
    > restriction/recommendation or has it never existed?


    The rationale is probably that user headers could change the way the
    standard headers are parsed, leading to unexpected and difficult to trace
    behavior. This may happen e.g. if user headers (re)define macros.
     
    Fred Zwarts, Jul 8, 2009
    #3
  4. Pavel

    Pavel Guest

    Fred Zwarts wrote:
    > "Pavel" <dot_com_yahoo@paultolk_reverse.yourself> wrote in message news:k8U4m.2852$...
    >> I vaguely remembered reading something in the Standard while ago that
    >> somehow discouraged the inclusion of user headers before Standard C++
    >> Library headers. I tried to refresh my memory and looked up in the
    >> Standard but could not find anything. Does anybody know any such
    >> restriction/recommendation or has it never existed?

    >
    > The rationale is probably that user headers could change the way the
    > standard headers are parsed, leading to unexpected and difficult to trace
    > behavior. This may happen e.g. if user headers (re)define macros.
    >

    Thanks Fred, also Ali and Hendrik,

    I remember reading some reasoning similar to the one of Fred's. I
    usually include standard C++ headers first, then standard C, then
    platform, then 3rd-party libraries then company libraries/mine, and the
    module's hearder at the end.

    I have been doing it for decades and I was sure I was doing it for a
    good reason -- I guess I need to re-think the whole thing on occasion.

    Now the only reason I can think of is that the internals of "more
    distant" files (standard etc) are less known to the programmer whereas
    their interface (effect of inclusion) is probably better defined and
    supposedly is kept to more rigorously. That is, the probability,
    expected negative impact, detection cost and fixing cost of messing up
    standard headers with my headers (or, especially unpleasant, with
    3rd-party headers from some really large and complicated library) seem
    to be all higher then those of messing up my headers (or 3rd-party
    headers) with standard headers. Also, when standard headers can include
    other standard headers, and there are #ifdef guards agains double
    inclusion, including standard headers first would ensure lesser maximum
    depth of inclusion.

    The above is not a strong opinion, just trying to get together some pros
    and contras now that I know my memory of the Standard recommendation was
    all false.

    -Pavel
     
    Pavel, Jul 9, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    9
    Views:
    541
    Andre Kostur
    Aug 25, 2003
  2. Matthew Burgess

    C++ Header Inclusion

    Matthew Burgess, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    2,089
    Mike Smith
    Aug 28, 2003
  3. Johannes Bauer

    Header inclusion question

    Johannes Bauer, Dec 7, 2004, in forum: C++
    Replies:
    6
    Views:
    432
    EventHelix.com
    Dec 8, 2004
  4. qazmlp

    Header Inclusion style

    qazmlp, Jul 8, 2003, in forum: C Programming
    Replies:
    14
    Views:
    557
    Casper H.S. Dik
    Jul 10, 2003
  5. Johannes Bauer

    Include guards and inclusion order

    Johannes Bauer, Dec 1, 2007, in forum: C++
    Replies:
    18
    Views:
    644
    James Kanze
    Dec 3, 2007
Loading...

Share This Page