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