473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?
Jan 20 '06 #1
7 2198

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 '06 #2
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.

Jan 20 '06 #3

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

Jan 20 '06 #4
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)
Jan 20 '06 #5
On Fri, 20 Jan 2006 16:06:10 +0100, Gabriel <ab***@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
No**********@Home.com
Jan 20 '06 #6
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

Jan 21 '06 #7
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?
Jan 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Nicolas Fleury | last post by:
In the following example: class MyMetaclass(type): pass class MyBaseType(object): __metaclass__ = MyMetaclass class MyType(MyBaseType): x = 4 y = 5 z = 6 Is there any way to modify...
8
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file?...
4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
10
by: Chris Gordon-Smith | last post by:
I am currently revisiting the code of a C++ application and restructuring the code where appropriate to make it consistent with the overall logical design. As part of this, I am looking at the...
10
by: sqlgoogle | last post by:
Hi I'm trying to update a db based on the select statement which has ORDER BY in it. And due to that I'm getting error which states that Server: Msg 1033, Level 15, State 1, Line 13 The ORDER...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
7
by: Arnold | last post by:
Greetings Gurus, In a mainform's header, I have a combobox named comboStudents. The rowsource for this combobox is: SELECT -999 As StudentID, "<Add New Student>" As FullName, "aaa" As...
5
by: tshad | last post by:
I have a PageInit.ascx that I want to put in all my pages and have it execute only once during the "not IsPostback" section. I also need it to execute first before anything else. I have it set...
12
by: badvoc | last post by:
Hi, I have had some good fortune on this site so I am back and I must iterate I am a beginer. I am having some problems getting to grips with the right technique to manage variables and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.