0

I mean to write a templated function, which should be available for a somewhat large number of typenames. So, I mean to avoid needing explicit instantiation.

In this case, is it mandatory to write the definition of the function in the header?

Is there any other way? (It would be used in several places, so the definition cannot be written in any single source file).

PS: Why can templates only be implemented in the header file? refers to templated classes. Even though it may be reasonable to think that the same applies to functions, perhaps sometimes, it may not be the case. Thus, the link does not answer this specific question.

  • This question provides the answer to your first question (yes) and suggests "alternatives" (having a separate file for definitions and `#include`ing it at the end of tyour header). – Yksisarvinen Feb 06 '20 at 22:37
  • *Why* do you not want to write the definition in a header? – walnut Feb 06 '20 at 22:50
  • @Yksisarvinen - I had read that question and answers before, and I have actually upvoted. But... 1) Things change over time and standards. Given the question and most of the highly voted answers are quite old, they may be partially correct, as of now. 2) The question refers to templated classes. Even though it may be *reasonable* to think that the same applies to functions, perhaps sometimes, it may not be the case. Thus, the link does not answer this specific question. – sancho.s ReinstateMonicaCellio Feb 06 '20 at 22:59
  • @walnut - E.g., https://stackoverflow.com/questions/2351148/explicit-template-instantiation-when-is-it-used – sancho.s ReinstateMonicaCellio Feb 06 '20 at 23:01
  • @sancho.sReinstateMonica Which of the given reasons are you referring to? E.g. the one in the accepted answer has nothing to do with where the definition is located, only that explicit instantiation in itself can be useful for library code. If that is the reason, then you need to write the explicit instantiations anyway though. – walnut Feb 06 '20 at 23:05
  • @walnut - I have two reasons to not have the definition in the header: 1) To take the liberty of sharing the code of the definition or not, 2) (less important) To have a cleaner header. Note: The question I linked is actually different... It is equivalent to this OP *only if* the answer to "is it mandatory..." is YES. – sancho.s ReinstateMonicaCellio Feb 06 '20 at 23:11
  • The function template's definition must be instantiated for every specialization used by the program, either implicitly or explicitly. It can only be instantiated if the definition is available in the translation unit. So if you don't want to instantiate explicitly in your library code and there are specializations that might be used by user code, then you need to provide the definition to the user code (conventionally in a header file). In that sense the answer to your first question will be "yes". – walnut Feb 06 '20 at 23:22
  • You can split the definition in another file that is included in the header (conventionally something ending in `.tcc` or `.tpp` or similar). This way the header is still clean. – walnut Feb 06 '20 at 23:25

0 Answers0