4

What do you recommend to do in such situation, when for instance you need to use functions from <algorithm> in both header file and source file?

example.hpp:

#include <algorithm>
#include <vector>

class Example
{
public:
  void definitionInHeader() { std::fill(m_values.begin(), m_values.end(), 10); }
  void definitionInSource();
private:
  std::vector<int> m_values(10);
}

example.cpp:

#include "example.hpp"

void Example::definitionInSource()
{
  //Code from <algorithm> is available here due to inclusion of example.hpp
  std::vector<int> integers{ 1, 2, 3, 4, 5 };
  auto print = [](int i){ std::cout << i << std::endl };
  std::for_each(integers.begin(), integers.end(), print);
}

Do you recommend to explicitly include in source file as well or maybe include only in header file? In my opinion, additional inclusion cost is meaningless, but if someone in the future will refactor such code and remove from header file, source file will have to be edited as well. On the other hand, include section in source file may get a bit long. What do you recommend?

Kasata Ata
  • 365
  • 3
  • 11
  • Good question. No certain answer possible. Even if you recommend that the header files be included in both, how can you make sure that you are sticking to that guideline? – john Aug 26 '19 at 10:06
  • If the content of a header file needs to use a standard header (e.g. in your example, for a class member that is of type `std::vector`, or an inline function that uses a standard algorithm) then it is preferable for that header to `#include` the standard headers. If including your header forces a developer to also `#include` standard headers - and will not compile unless those standard headers are included - all you achieve by leaving the standard headers out is more effort for developers to use your headers. – Peter Aug 26 '19 at 10:19
  • @Peter I guess that the question is meant in a different way. Of course the class header needs to include the standard headers. But since the class implementation file (the .cpp file) **always** has to include the class header file, inclusion of standard header files would **always** be some kind of repetition. Does this repetition have any advantages? – Benjamin Bihler Aug 26 '19 at 10:57
  • @BenjaminBihler - well, no, there are few advantages of repetition, and there can be disadvantages. That's why there are guidelines like DRY. While DRY doesn't need to mean "eliminate all repetition" (which too many people try to do, and succeed in making their code unnecessarily hard to maintain), too much repetition tends to be a maintenance burden in the long run. – Peter Aug 26 '19 at 11:18
  • @Peter I think you are twisting the question a bit too much... The question is not about the obvious time where your header needs to include another one. Question is, if both the header and the source needs ``, should you include `` in both the header and the source? Or only in the header since the source includes the header? – Holt Aug 26 '19 at 11:24
  • @Holt @BenjaminBihler Clearly, I got the wrong end of the stick, here! Answer wiped. Yes, it is a reasonable question. Presumably, the standard headers will have mechanisms in them to implement the `#pragma once` type of thing? – Adrian Mole Aug 26 '19 at 11:28
  • Hmmmmmm... if the answer to that question was primarily opinion-based, then there would be no clear technical advantage or disadvantage of including all standard headers in the class implementation file. But in the absence of a clear advantage, the DRY rule obviously comes into play. Isn't the answer "Don't repeat standard header file inclusions in class implementation files!" therefore the correct, non-opinion based answer? – Benjamin Bihler Aug 26 '19 at 12:59

0 Answers0