Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   order of #include statements (http://www.velocityreviews.com/forums/t451221-order-of-include-statements.html)

Gabriel 01-20-2006 03:06 PM

order of #include statements
 
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?

roberts.noah@gmail.com 01-20-2006 03:11 PM

Re: order of #include statements
 

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.


Rolf Magnus 01-20-2006 03:16 PM

Re: order of #include statements
 
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.


Gavin Deane 01-20-2006 03:27 PM

Re: order of #include statements
 

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


Pete Becker 01-20-2006 03:52 PM

Re: order of #include statements
 
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)

Bob Hairgrove 01-20-2006 04:30 PM

Re: order of #include statements
 
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
NoSpamPlease@Home.com

Jay Nabonne 01-20-2006 11:51 PM

Re: order of #include statements
 
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


Gabriel 01-24-2006 08:37 AM

Re: order of #include statements
 
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?


All times are GMT. The time now is 07:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.