Best practices for use of C++ header files [closed]

泄露秘密 提交于 2019-11-27 18:34:56
  1. The order of the include guards and the comments is purely a matter of style - it won't have any measurable effect on speed of compilation.

  2. Namespaces absolutely should be used in header files for declaring functions, classes, globals, etc. What you should not do is use using statements in header files -- it's impossible to unuse something in a source file that includes it, and you shouldn't force includers to add extra stuff to the global scope. If you need to use things from other namespaces in your headers, fully qualify every name. It can be a pain sometimes, but it's really the right thing to do.

Examples:

// WRONG!
using namespace std;
class MyClass
{
    string stringVar;
};

// RIGHT
class MyClass
{
    std::string stringVar;
};

As for forward declarations of classes in other namespaces, you've got it exactly right. Just remember to always qualify AnotherFoo as AnotherNameSpace::AnotherFoo when you reference it inside your header. Indeed, forward declarations are the only way to break cyclic dependencies.

  1. I've heard that having comments before the include guard can cause some compilers to miss an optimization. If the guard is the very first thing, the compiler may recognize the idiom and not even bother opening the header for subsequent includes. In my own code, comments normally precede the include guard. I haven't ever bothered testing to see if this has any impact or not. And I likely never will (but if someone else does, I'd be interested in the results).

  2. Of course headers should incorporate namespaces - otherwise nothing useful could ever be inside a namespace. However, as you mentioned, headers should not 'import' (for want of a better word) namespaces into a compilation unit with a 'using' directive.

Regarding #1, I am not aware of any concrete argument for or against. Many companies have a policy where the copyright notice has to be the first item in the file BEFORE anything else or any meaningful code (maybe the assumption is that you'll read the copyright before you absorb any code). For that purpose, #IFNDEF is already code. From a usability point of view, it makes sense to put the copyrights first because the eye ignores them. However, anything describing the module should come after the #ifndef in my opinion.

1) Since comments don't actually do anything, I doubt it matters much. Technically though, #include copy and pastes, so putting the comments outside the header guards can mean more work for the preprocessor. I don't know if most compilers are smart enough to optimize for this (i.e. if they strip comments before the preprocessor step) but you probably wouldn't notice until you reached tens of thousands of header files.

2) That's correct. If you want to put a class inside a namespace, and that class is going to get declared in a header file, well then it should be declared inside the namespace, which thus should be in the header file. And yes, that's how you forward declare exactly. And yes, it is the main tool for avoid cyclic dependency (you could also change your design, but there's nothing wrong with cyclicness in principle provided the two classes in question are only referring to each other by reference or pointer and not calling any methods).

  1. I don't think adding comments has any performance impact as pointed by Adam's answer to this post.

  2. I have used myself namespaces in header files, what if you define you own string class , so it would clash with the std namespace string class.

Using the "using" keyword exactly is not wrong (since it gives you a lot of convenience and lot less to type before all your variables)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!