order of #include statements

G

Gabriel

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
 
R

roberts.noah

Gabriel said:
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.
 
R

Rolf Magnus

Gabriel said:
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.
 
G

Gavin Deane

Gabriel said:
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
 
P

Pete Becker

Rolf said:
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.
 
B

Bob Hairgrove

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.
 
J

Jay Nabonne

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
 
G

Gabriel

Gabriel said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top