order of #include statements

Discussion in 'C++' started by Gabriel, Jan 20, 2006.

  1. Gabriel

    Gabriel Guest

    Hi

    I have heard that is advisable/advantageous to #include the standard
    headers (<header>) before your own headers ("header.hpp") in your source
    file. Is this true? If it is: why is this so?

    Gabriel
    --
    Who is General Failure and why is he reading my hard disk?
    Gabriel, Jan 20, 2006
    #1
    1. Advertising

  2. Gabriel

    Guest

    Gabriel wrote:
    > Hi
    >
    > I have heard that is advisable/advantageous to #include the standard
    > headers (<header>) before your own headers ("header.hpp") in your source
    > file. Is this true? If it is: why is this so?


    I don't see how it could make a bit of difference because your headers
    should include what they need. I personally prefer to group my header
    includes based on where they come from but that is totally a style
    thing...the only "technical" reason is I think it is easier to make
    sense of that way.
    , Jan 20, 2006
    #2
    1. Advertising

  3. Gabriel

    Rolf Magnus Guest

    Gabriel wrote:

    > Hi
    >
    > I have heard that is advisable/advantageous to #include the standard
    > headers (<header>) before your own headers ("header.hpp") in your source
    > file. Is this true? If it is: why is this so?


    I prefer to do it in just the opposite order. This makes sure that my
    headers are self-contained, i.e. I get an error if my header depends on a
    standard header but doesn't itself #include that header.
    Rolf Magnus, Jan 20, 2006
    #3
  4. Gabriel

    Gavin Deane Guest

    Gabriel wrote:
    > Hi
    >
    > I have heard that is advisable/advantageous to #include the standard
    > headers (<header>) before your own headers ("header.hpp") in your source
    > file. Is this true? If it is: why is this so?
    >
    > Gabriel


    I've never heard it that way round. I've heard it the other way -
    within each source file, include your own headers before the stadard
    headers. That way, if your own headers themselves use parts of the
    standard library, you (well, the compiler) are more likely to catch the
    case where you accidentally forget to include a standard header in your
    own header. For example:

    // my_header.h
    #ifndef MY_HEADER_H
    #define MY_HEADER_H

    class my_class
    {
    public:
    std::vector<int> v;
    };

    #endif
    // end of my_header.h
    // oops - forgot to #include <vector>

    _____________________

    // my_program.cpp
    #include <vector> // my program uses vectors a lot
    #include "my_header.h"

    int main()
    {
    // lots of clever stuff ...
    }

    The above will compile. Switch the orders of the includes in
    my_program.cpp and it won't.

    In any real project, my_header could include other headers, which
    themselves might include others (standard ones or my own ones) again.
    my_program will probably include other things too. So the chances are
    that <vector> could still be indirectly included before my_header.h and
    my mistake would be hidden.

    It's not a fool-proof technique by any means, but it does no harm and
    does offer a certain amount of protection so I prefer it.

    Gavin Deane
    Gavin Deane, Jan 20, 2006
    #4
  5. Gabriel

    Pete Becker Guest

    Rolf Magnus wrote:
    >
    > I prefer to do it in just the opposite order. This makes sure that my
    > headers are self-contained, i.e. I get an error if my header depends on a
    > standard header but doesn't itself #include that header.
    >


    Well, you might get an error, but you might not. Depends on what any
    headers that precede the one in question do.

    Generally speaking, if you want to be sure that your headers include
    everything they need, compile 'em by themselves.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jan 20, 2006
    #5
  6. On Fri, 20 Jan 2006 16:06:10 +0100, Gabriel <abuse@127.0.0.1> wrote:

    >Hi
    >
    >I have heard that is advisable/advantageous to #include the standard
    >headers (<header>) before your own headers ("header.hpp") in your source
    >file. Is this true? If it is: why is this so?
    >
    >Gabriel


    I hardly ever find the need to include standard headers in a source
    file. Usually, I include all the standard headers I need in a single
    ..hpp file which is included by my other headers. Makes it easier to
    use precompiled headers, too.

    --
    Bob Hairgrove
    Bob Hairgrove, Jan 20, 2006
    #6
  7. Gabriel

    Jay Nabonne Guest

    On Fri, 20 Jan 2006 16:06:10 +0100, Gabriel wrote:

    > Hi
    >
    > I have heard that is advisable/advantageous to #include the standard
    > headers (<header>) before your own headers ("header.hpp") in your source
    > file. Is this true? If it is: why is this so?
    >


    The only time we had to do this was when "our own" header files #defined
    a macro that broke the standard header files.

    - Jay
    Jay Nabonne, Jan 20, 2006
    #7
  8. Gabriel

    Gabriel Guest

    Gabriel wrote:
    > Hi
    >
    > I have heard that is advisable/advantageous to #include the standard
    > headers (<header>) before your own headers ("header.hpp") in your source
    > file. Is this true? If it is: why is this so?
    >
    > Gabriel


    Thanx for all the opinions and informations,
    Gabriel

    --
    Who is General Failure and why is he reading my hard disk?
    Gabriel, Jan 24, 2006
    #8
    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. Neil Zanella
    Replies:
    8
    Views:
    1,167
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    362
    Bart Nessux
    Feb 23, 2004
  3. Vince
    Replies:
    12
    Views:
    737
    Martin Gregorie
    Jan 21, 2008
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    879
    Andreas Bogenberger
    Feb 22, 2008
  5. John Crichton
    Replies:
    6
    Views:
    253
    John Crichton
    Jul 12, 2010
Loading...

Share This Page