Organize includes

后端 未结 6 1103
臣服心动
臣服心动 2020-12-31 00:14
  • Is there some preferred way to organize ones include directives?
  • Is it better to include the files you need in the .cpp file instead of the
6条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 00:47

    1. You should have them at the top of the file, all in one place. This is what everyone expects. Also, it is useful to have them grouped, e.g. first all standard headers, then 3rd-party headers (grouped by library), then your own headers. Keep this order consistent throughout the project. It makes it easier to understand dependencies. As @James Kanze points out, it is also useful to put the header that declares the content first. This way you make sure that it works if included first (meaning it does no depend on any includes that it does not include itself).
    2. Keep the scope as small as possible, so that a change in the header affects the least number of translation-units. This means, whenever possible include it in the cpp-file only. As @Pedro d'Aquino commented, you can reduce the number of includes in a header by using forward declarations whenever possible (basically whenever you only use references or pointers to a given type).
    3. Both - explicit is better than implicit.
    4. After some reading, I believe you should only include headers in the PCH if you are confident that they do not change anymore. This goes for all standard headers as well as (probably) third party libraries. For your own libraries, you be the judge.

提交回复
热议问题