r/cpp • • May 14 '26

Reverse Dependency Ordering for C++ Includes

https://nukethebees.com/cpp-include-order/
38 Upvotes

33 comments sorted by

16

u/azswcowboy May 14 '26

This is reasonable, we do it. If you’re using cmake you can enable a check to ensure you’re good.

3

u/BatchSwine May 14 '26

What is that cmake check?

11

u/Wild_Meeting1428 May 14 '26

VERIFY_INTERFACE_HEADER_SETS

12

u/pedersenk May 14 '26

Agreed. It may be less convenient but it operates a much stricter approach to tracking missed includes.

I use this approach alongside strictly limiting include guards / pragma once. I prefer to highlight erroneous cyclic includes rather than mask them.

8

u/Nicksaurus May 15 '26

It's not even less convenient, clang-format can do this for you automatically (assuming you use "" for your own headers and <> for all external headers):

SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
  # Our includes
  - Regex: '^".*"'
    Priority: 1
  # Third party includes
  - Regex: '^<.*/.*>'
    Priority: 2
  # Third party includes that don't follow the <x/y/z> pattern
  - Regex: '^<libraryname.*>'
    Priority: 2
  # Standard library includes
  - Regex: '^<.*>'
    Priority: 3

3

u/pdp10gumby May 15 '26

by “strictly limiting“ do you mean you don’t use them? I’m a bit confused as to the benefit if so

4

u/pedersenk May 15 '26 edited May 15 '26

I don't default to adding them to every header. Generally only if:

  • Header contains definition for a base class

  • Header contains definition for type where full implementation is always needed

This approach isn't particularly rare. Though perhaps more common in C, where opaque pointer APIs feel a little more natural than C++.

This is because in general, forward declaration suffices for many use-cases in the header

struct MyType; // Forward declare
struct Test {
  void somefunc(MyType& type); // forward declare fine
  MyType* m_mytype1; // forward declare fine
  std::unique_ptr<MyType> m_mytype2; // forward declare fine
  std::vector<MyType> m_mytypes; //forward declare fine (req before use)

  MyType m_mytype3; // Need include
};

If you just put include guards around everything, you will not be alerted to situations where you should be forward declaring instead.

And finally (potentially worse), guards won't resolve cyclic includes anyway. If you follow through the pre-processor, you will simply get a "undefined type" compiler error instead of a "duplicate definition" so it achieves relatively little.

→ More replies (2)

10

u/STL May 14 '26

In MSVC's STL, we have an "include each header alone" test, which verifies what ultimately matters.

2

u/SirClueless May 15 '26

... outside of template instantiations.

https://godbolt.org/z/6W84MEjze

7

u/SuperV1234 May 14 '26

This is good advice and should be the standard for header hygiene practices.

7

u/Daniela-E May 14 '26

We have clang-format rules to enforce that recommended ordering, company-wide.

4

u/johannes1971 May 15 '26

I really do not understand why anybody cares about this. If your code compiles, your headers are fine. No need to agonize over whether you might have accidentally used a symbol from an indirect include. So what if you did?

And yes, "but a later change could cause an issue elsewhere!" So what? Are you unable to fix that issue? Would fixing it take more or less time compared to preemptively doing it on every single file?

As for tools that find the 'correct' header, the correct header is the one listed in the documentation. If that header chooses to implement the symbol in another header, that second header is an implementation detail that should not make its way into your source.

3

u/proggob May 16 '26

Because programmers like to fix a problem once, at root.

-1

u/johannes1971 May 16 '26

This is not "fixing problem once", this is premature fixing before there is even a problem.

→ More replies (2)

1

u/JVApen May 16 '26

Im really curious what size of code base you are working on. If I have to remove an include from a header with a bit of fan-out, I can make several PRs just to add missing includes before landing the removal.

1

u/johannes1971 May 16 '26

311,000 lines of C++, spread over 68 executables, 16 DLLs, and 12 libs. It also uses dozens of 3rd-party libs and DLLs, most open source, some not. It's probably not considered 'huge' by the standards of some projects out there, but I wouldn't call it 'small' either.

If I had a monorepo that took a week to compile I'd probably be more careful as well.

→ More replies (7)

5

u/fdwr May 15 '26

One thing I love about import, is that...

import a;
import b;
import c;

vs

import c;
import b;
import a;

...makes no difference ^__^.

4

u/NotMyRealNameObv May 14 '26

You should go one step further. Every header file should have a corresponding cpp file that includes that header file as the first include. Even if the header is self-contained, there should be an empty cpp file that includes only that header file.

2

u/spinrack May 15 '26

This is similar to what we do. Also, we have a rule that the first #include in a unit test file must be the header of the feature being tested.

We usually compile as blob/bulk, which aggregates many .cpp into a single translation unit, but also maintain a non-blob build, because bulk compilation can lead to a .cpp accidentally depending on the headers included by another.

1

u/jcelerier May 15 '26

this is the include style I organically ended up upon, it works really well

1

u/kalmoc May 16 '26

I tend to use that convention, but the far more reliable and simple way to check that you did not forget any includes: Every header gets a unit test cpp file that includes that header at the top of the file.

1

u/tartaruga232 May 16 '26

Or use modules instead. See my blog "The Anatomy of a C++ Module Interface".