norman
norman

Reputation: 5556

Does the order of #include directives and "using" statements matter at the beginning of a C++ header file?

I am cleaning up a C++ header file and have noticed something like the following:

#if !defined(HEADER_H_)
#define HEADER_H_

#include <vector>
#include <string>

using namespace std;

#include<stdio.h>

#include "Blar/ObjA/Model.h"

namespace blar{
  class Blar;
}

#include <Blar/Blar.h>
#include <Blar/ObjB/OtherModel.h>

using namespace blar;

#include <Utilities/OtherThing.h>  
#include <qstringlist.h>

Is this just bad practice, or do some of the ramifications of each #include/using/namespace related declaration actually depend on the order? Since there is no code in between, I wouldn't think so, but I'm not familiar with too many subtleties...

Upvotes: 1

Views: 903

Answers (3)

Raju
Raju

Reputation: 1169

The order of #include's and using's doesn't matters. But it's not a good programming practice as the code becomes less readable. So write all the #include's at one place and all the namespaces at one place.

Ex:

#include "stdio.h"
#include "math.h"

using namespace std;
using namespace xyz;

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76235

If the headers are properly written it doesn't matter. If they have inter-dependencies it makes a great deal of difference.

// header 1
#undef FOO
#define FOO 1

// header 2
#undef FOO
#define FOO 2

That's a silly example, but it's fairly easy, if you're not careful, to get similar conflicts without using the preprocessor.

Upvotes: 1

Nico Cvitak
Nico Cvitak

Reputation: 471

It really doesn't matter which includes you put first, so long as all of the includes are within the#if !defined(HEADER_H_), but I like to put all of my standard libraries at the first,however this is entirely up to you.

Upvotes: 0

Related Questions