Search code examples
c++includeheader-filesdeclarationusing

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


Solution

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