1

Here's a scenario:

suppose you have #include <unordered_map> in the header and you don't declare it anywhere in the implementation file

but you use it, maybe

mp[whatever] = some;

should you re-include the <unordered_map> header? what is best practice?

in general, what's the best practice w.r.t relying on transitive includes like this, assuming that those things you are relying on are perhaps part of the method type signatures or member variables?

to be more explicit:

std::unordered_map<>

might occur in the header

but no way does the type appear in the implementation. An object of that type is only used.

8
  • 3
    Does this answer your question? Is it good practice to rely on headers being included transitively? Commented May 11 at 5:25
  • 1
    Does this answer your question? Is it good practice to rely on headers being included transitively? Commented May 11 at 6:19
  • @BartvanIngenSchenau: did you notice that the other question (and its answers) does not say anything about sys-lib headers? All examples shown there are about project-immanent headers. I was considering to vote as a dupe, too, but I hesitated exactky because of this difference.
    – Doc Brown
    Commented May 11 at 6:38
  • @DocBrown, that difference is inconsequential. Commented May 11 at 6:44
  • 1
    @BartvanIngenSchenau: there is a difference - headers from standard libs don't change (or at least very rarely), so the negative effect of transitive includes to build times will less matter than in the case of project-immanent headers. This might be irrelevant for this case, but I think it is not necessarily obvious for the asker that this is the case. That other question is related, of course, but Ben Cotrell's comment seems to be enough for my taste - I would not have close-voted this as a dupe.
    – Doc Brown
    Commented May 11 at 15:04

1 Answer 1

5

When

  • a header file A.h requires an include for <unordered_map>, and

  • the related cpp file A.cpp (which includes A.h) contains some code dealing with objects of type unordered_map (regardless whether that type is written explicitly in the cpp or not)

there is no clear consensus in the C++ community whether A.cpp should include <unordered_map> as well or not.

Source: David Kieras,, C++ Header File Guidelines (2015), see page 3, Guideline #12. That paper also explains the two possible point of views, either

  • the two includes are redundant, or

  • readability should be favored over redundancy in this case

IMHO both POV can be justified. I would recommend to pick one of the two conventions and stick to it throughout your code base.

3
  • Thank you. What was the thing you mentioned in your comment about it being a system header and transitive not affecting build time once? I’m not sure what transitivity has anything to do with build times, given the inclusion is handled by the preprocessor. Commented May 11 at 18:17
  • And the preprocessor doesn’t take any time?
    – gnasher729
    Commented May 12 at 8:09
  • @user129393192: in C++, when you don't follow a few restricting rules in regards to header files and includes (they are mentioned in the paper I linked to), you can easily end up with one little change in the wrong header file causing a cascade of files which need to be recompiled and relinked. In a small program, this won't matter, but in a program with >1000 classes (each one with a header and a cpp file) this can cause build timesof several minutes, if not hours.
    – Doc Brown
    Commented May 12 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.