Which comes first? header guards, namespace and includes [closed]
Want to improve this question? Update the question so it can be answered with fac开发者_Go百科ts and citations by editing this post.
Closed 4 years ago.
Improve this questionI have been making files like this for awhile: Does the order make sense? or should the namespace and the #includes be swapped and why.
#ifndef CLASSNAME_H // header guards
#define CLASSNAME_H
#include "a.h" // includes in alphabetical order
#include "b.h" // user specified includes first
#include "c.h"
#include <vector> // then library includes
namespace MyNamespace
{
class ClassName
{
};
}
#endif
Yes. That's looks good.
Though I order my headers differently (but alphabetically is fine).
The only thing I would change is the include guard. I make the include my namspace as well as the class name. As several times I have classes with the same name (but in a different namespace) being used by the same code.
#ifndef MY_COMPANY_MY_NAME_SPACE_MYCLASSNAME_H // header guards
#define MY_COMPANY_MY_NAME_SPACE_MYCLASSNAME_H
#include "a.h" // includes in order of most specific to most general.
// My includes first.
// Then C++ headers <vector>
// I group all the containers together.
// Then C specific headers <sys/bla.h>
// Then C generic headers <ctype.h>
namespace MyNamespace
{
Class ClassName
{
};
}
#endif
What you've written is perfect. I don't think you need to change the order.
精彩评论