Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > order of #include statements

Reply
Thread Tools

order of #include statements

 
 
Gabriel
Guest
Posts: n/a
 
      01-20-2006
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?
 
Reply With Quote
 
 
 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      01-20-2006

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.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-20-2006
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.

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-20-2006

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

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-20-2006
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)
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      01-20-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Jay Nabonne
Guest
Posts: n/a
 
      01-20-2006
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

 
Reply With Quote
 
Gabriel
Guest
Posts: n/a
 
      01-24-2006
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
Order of statements tshad HTML 3 03-02-2005 08:16 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM



Advertisments