-1

Which is the recommended order in which the #include directives are supposed to be listed? I could not find any answer in the C++ Core Guidelines

For example, should they be ordered like this:

#include "OtherHeaderInCurrentLib.h"
#include <third_party_library/header.h>
#include <iostream>

Or should they be ordered like this:

#include <iostream>
#include <third_party_library/header.h>
#include "OtherHeaderInCurrentLib.h"

And is there any difference between the recommended ordering when they are listed inside a source files and when they are listed inside a header files?

8
  • 2
    Alphabetically sounds sound Commented Jan 25, 2021 at 17:15
  • I think if your headers include everything that they need this is mostly user preference. Commented Jan 25, 2021 at 17:18
  • 1
    "I doubt since language features such as ADL heavily rely on the declaration order" This is a clear sign, that these headers are written in a wrong way. The order shouldn't matter at all. The client should only #include what's actually needed. Commented Jan 25, 2021 at 17:22
  • 1
    For me working with precompiled headers on msvc (for 2+ decades) it behaves much like your second example for source files. I mean inside the pch.h I usually include the system headers and probably the third party headers but then have to include the pch.h as your first include since that is a requirement for your source files as the precompiled header implementation has the compiler ignoring all lines above #include "pch.h" Commented Jan 25, 2021 at 17:23
  • 1
    This is destined for an opinion-close, but I'm gonna give mine. Put yours first. It greatly assists in avoiding consumption-based inclusion dependencies. I.e. If your header requires <string>, it should include <string>. If you don't, but put it last after a stack of includes in a cpp that has #include <string>, that mishap is hidden... until you use it somewhere else where the consuming cpp didn't include <string> before your header, and now you're wondering wtf.. this worked before, why not now? In short, I agree with eerorika. Commented Jan 25, 2021 at 17:24

1 Answer 1

3

A disadvantage of including standard, or third party headers before first party custom headers is that it will be very easy to accidentally depend on headers which you forgot to include directly. This will cause problems when the indirect inclusion is removed due to unrelated changes, or the custom header is included into context that doesn't have that missing dependency.

In the typical x.hpp, x.cpp "module" structure (not to be confused with C++20 modules), including x.hpp as the first header in x.cpp ensures that all such headers will have at least one translation unit where they are included first, avoiding the problem of undetected missing inclusions within x.hpp.

Other than that, it is a good idea to keep the inclusions in order so that it is easy to see what the dependencies are at a glance. Grouping by library, and sorting groups alphabetically are typical ways to keep the inclusions in order.

Sign up to request clarification or add additional context in comments.

5 Comments

fwiw, google guideline recommends to put your projects headers last: google.github.io/styleguide/… arguing that putting foo.h first in foo.cpp is sufficient to make it break in case of missing includes while others using foo.h shouldnt be bothered with the error.
@largest_prime_is_463035818 this is what I was looking for, some industry standard recommendation with reasons behind the choice. Unfortunately, as always they have to rush the close of any question to get their beloved tokens, otherwise I would have chosen your answer.
@largest_prime_is_463035818 That's a reasonable argument. They seem to not explain why it would be advantageous to include rest of the custom headers after the standard headers though. As far as I can tell, it doesn't matter.
@nyarlathotep108 It's not an industry standard. It's a Google style guide.
@nyarlthotep108 yes consider that the google guideline was written with a very specific code base in mind. Its different problems you face when you have to manage millions of lines of code somehow. Once in a while I find a bullet point in their guideline, that I simply do not want to apply in my projects (but I understand why they put it in their guideline)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.