2

Due to the implementation of a template class in a header file, I have to include the other includes used in the implementation of the class in the header file.

Because of that, each time I include my template class that bring all other the inclusions and so on.

This could bring to an eccessive spread of inclusion, or even unnecessary for the context.

So what's the best way to deal with this issue?

Edit: Since I've not explictely mentioned it, I'm not talking about cases where forward declaration could solve it like in a normal .h/.cpp separation, but when you have to include the header, and since you don't have the cpp, you are force to use it in .h

Edit 2: Let's say my template class has a function with a dependency with a third class library. Each class using my template class now has the same depency, or can access to that header which both I could not want to. Does it exists a way to avoid that?

Moia
  • 2,216
  • 1
  • 12
  • 34
  • Forward declarations probably ... but if people always need to include the header for XYZ to be able to use your template; then just include it in the header. – UKMonkey May 04 '18 at 13:34
  • I dont understand why you consider this as an issue. If the header that declares the template has unnecessary includes, then just remove them, if they are necessary, then ...well then they are necessary – 463035818_is_not_an_ai May 04 '18 at 13:34
  • 2
    Well, this is the downside of templates which they are trying to solve with Modules for C++20... That's the way it is, go with it. – DeiDei May 04 '18 at 13:36
  • [Include guards](https://en.wikipedia.org/wiki/Include_guard) or [`#pragma once`](https://en.wikipedia.org/wiki/Pragma_once) should keep it so that header files are not actually included multiple times in each [translation unit](https://en.wikipedia.org/wiki/Translation_unit_(programming)). – Some programmer dude May 04 '18 at 13:36
  • Roughly speaking, your headers will only need to include implementations of templates you use, any templates they rely on, and declarations of non-template code (function declarations, type definitions, etc) that the templates rely on. The usual strategies are as for any header file: avoid unnecessary dependency, avoid "kitchen sink" header files that bundle up lots of unrelated functions/template, use include guards (particularly if headers need to include each other), etc etc. – Peter May 04 '18 at 13:46
  • This is the way C++ goes, until up to C++17... After all, C++ is a compiled language, meaning that compilation time is not an end user problem, it only bothers the developper. – Serge Ballesta May 04 '18 at 13:59
  • Let's say we have an header file containing debug function, very specific function or even worse **external library inclusion**. I just don't want each file which include the template classe has access to debug function, or very specific function, or even worse a **not wanted dependency with an external library** which could be solved just adding it in a cpp file. – Moia May 04 '18 at 15:03
  • 1
    About the only solution I can think of is granularity. If only part of your template functionality depends on another header, split the template so that the users who don't need the extra don't have to include it. May not always be possible, of course. –  May 04 '18 at 15:15
  • Have you tried using #define before #include, then #ifdef inside include file, to selectively skip the code you don't always want to have in a compilation? Though this can lead to an ifdef mess... – hyde May 04 '18 at 15:23
  • 1
    @hyde a macro hell is even less desirable – Moia May 04 '18 at 15:25
  • @Moia Well, there's macro hell (where macros contain code), and there's define hell (where macros just determine what code is included/excluded in compilation). But yeah... But this is what you are essentially asking, "I sometimes want the code with dependencies, sometimes not". Another way to do this is to move all this code to a different .cpp, and have several versions of the .cpp, some of them containing dummy implementations without dependencies, so linking command decides which is used. – hyde May 04 '18 at 15:36
  • @hyde yes, I'm not looking for an universal solution, I was trying to understand if there's a way or a best practice to avoid the propagation in those situations it should be better not to. – Moia May 04 '18 at 15:43

1 Answers1

-1

Use forward declaration where ever appropriate instead of #include. Header file should #include only the needed and rest should go in source file.

Archie Yalakki
  • 512
  • 4
  • 12
  • 1
    "should go in source file" is not an option for templates – 463035818_is_not_an_ai May 04 '18 at 13:54
  • Since you need the header inclusion when you implement the methods and in a template class you have the implementation in the header, forward declaration does not help. – Moia May 04 '18 at 15:15
  • The only way I could think of is, breaking down the functionality and putting them in multiple header files with one namespace. So the client can include only one header file they need. – Archie Yalakki May 04 '18 at 15:32