哪个先来?标题保护,命名空间和包含

aCu*_*ria 16 c++ namespaces include-guards

我一直在制作这样的文件:订单有意义吗?或者应该交换名称空间和#includes以及原因.

#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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ork 10

是.那看起来不错.

虽然我以不同方式订购我的标题(但按字母顺序排列很好).

我唯一要改变的是包括后卫.我创建了包含我的namspace以及类名.有几次我有相同名称的类(但在不同的命名空间中)被相同的代码使用.

#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
Run Code Online (Sandbox Code Playgroud)

  • 这条规则的问题在于你必须与其他开发者争论,其中包括"更具体"和"更一般".因此,由于每个include都应该提取所有依赖项,因此顺序通常是无关紧要的,字母顺序是一个很好的选择,不会引起讨论(当然,在拆分项目和系统头之后). (2认同)

Naw*_*waz 5

你写的很完美。我认为你不需要改变顺序。