Should I repeat inclusions in .cpp and .h?
Asked Answered
M

4

10

Scenario:

foo.h:

#include <vector>

class foo {
  public:
  std::vector<int>* getVector();

  /* ... other methods declarations ... */

}

foo.cpp:

#include "foo.h"
#include <vector>

/* ... other methods definitions using std::vector ... */

std::vector<int>* foo::getVector() {
  return new std::vector<int>();
}

I want .cpp to be independent of any possible future changes in the header. If for whatever reason the interface of the class changes and the dependency from <vector> can be eliminated, I risk that other methods in the .cpp also lose that inclusion.

Is it correct to repeat the inclusion of <vector> in both the .cpp and .h? Does this practice make sense or should I just rely on the inclusions made in the header?

Many answered 24/7, 2012 at 21:46 Comment(11)
Include exactly what you need on a per-file basis. As a side note, you're missing template arguments.Cloudberry
Thanks for the suggestion, that is exactly what I was thinking. I don't understand what you mean by "you're missing template arguments"...Many
Vectors need a type: std::vector<int> v;Cloudberry
Yeah, you'd be better off leaving out the code, because it has nothing to do with the question and will invite a whole lot of comments about how it sucks.Levison
@keith.layne, True, but that does help in the future. For example, the pointers used here are unnecessary.Cloudberry
Too argumentative, think any good answers are going to get lost in the flamewar (which is too bad).Morose
@Cloudberry I know it's not the matter of the question, but just to learn: why do you say that pointers in that code here are not necessary?Many
@AndreaCasaccia, Oops, I somehow misread that as you had the vector as a member of your class and getVector was a getter. That would be unnecessary usage of pointers. With the non-misread code, it depends how you're using it.Cloudberry
@BenVoigt: Actually I don't think this argumentative at all, the options are clear: include in the header (only if needed) include in the cpp (only if needed), include in both (never needed, as if the header includes it, the cpp does not need it anymore). I guess that is just my opinion :)Sherer
@dribeas: Argumentative has nothing to do with having a finite number of possible answers.Morose
@BenVoigt you are probably right about the argumentative nature of the question. I would say that the discussion has been useful anyway, gathering various opinions helped me to make my own idea on the subject.Many
Q
18

Include what you need, and nothing more.

Including the same header file across multiple .h files and multiple .cpp files is not a problem in itself. Header guards are effective at mitigating problems from including files multiple times.

If you start trying to avoid including the same file multiple times, it can actually be negative as it usually leads to a "mega-include file" which includes everything you need in the entire project. This is bad, because one change to any header file causes everything to re-compile.

If you are worried about a .h/.cpp file both including the same file, then follow these guidelines:

  • If the include is not needed in the header file, only include it in the CPP
  • If a class declaration is needed in the header file (but not used), use forward declaration in the .h file and include it in the CPP file.
  • If you actually use the include within the header file, include it in the header file and not the CPP.
Quadricycle answered 24/7, 2012 at 21:52 Comment(2)
+1: to be more explicit, in your case and because you cannot forward declare std::vector, you would include it in the header and not in the cpp.Sherer
@TimothyShields, the bullet list is actually just adapted from Google's code guidelines for C++ google-styleguide.googlecode.com/svn/trunk/cppguide.xmlQuadricycle
A
1

No, you shouldn't. It serves no purpose. Redundant lines are cost without benefit.

Each file should include what it needs and no more. In your specific case of a header and its implementation, the redundant declaration in the .cpp doesn't help. If the header changed enough that the function no longer needed to return a vector you would need to revisit the .cpp anyway.

Atombomb answered 24/7, 2012 at 21:52 Comment(7)
-1. Following this advice would naturally lead to the mega-include file, which can drastically increases compile time. See my answer.Quadricycle
@Star How so? The .h should include only what it needs, the .cpp should include the .h first and then exactly what it needs. Duplication does not help.Atombomb
@Alan Actually the case I was trying to analyze with my question is when both .h and .cpp need the same file.Many
Ok, so think of it this way: Let's say 5 cpp files all include (and use) th same 3 header files. According to a literal interpretation of what you said, I should move those 3 includes to a "general purpose include" file to avoid duplication right? Wrong. Doing so tempts people to include the meta-include file in leu of the actual files, increasing compile time for small changes.Quadricycle
Adding more, based on your comment, it seems that you intrinsically know how to do it, but your answer suggests that you have adopted a policy that is far too broad.Quadricycle
@star The question specifically asks about foo.h and foo.cpp, not bar.cpp. FWIW I entirely agree with your answer, but I see no contradiction in mine.Atombomb
Like I said, you have an understanding, but my interpretation of your answer (by itself) paints too broad of a picture. Add another sentence our two to clarify and I'll happily switch to an upvoteQuadricycle
E
1

In .cpp file, it is enough to include only things specific for implementation (what .cpp file in fact is), without repeating stuff you already included in header. This way, when somebody is looking at your code, he also gets a better and cleaner understanding of your code.

It can be very useful to know which dependencies are specific only to implementation, for example when you are upgrading/replacing it with another one (while preserving the interface).

Experiential answered 24/7, 2012 at 21:53 Comment(0)
A
1

Include as few files in the header as possible, and only include them in the .cpp files that need them. Your foo.h header file might be included in many other .cpp files that do not need the declarations from the other header file(s) (in this case vector.h), which in the long run leads to longer compilations and less clear code.

Angwantibo answered 24/7, 2012 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.