Answer the question
In order to leave comments, you need to log in
Declaring and Defining a Method Template?
Guys, there was a question whether it is possible to throw the declaration of a template class method into a .h file, and its definition into another file or all in one? Sorry in advance for the stupid question
Answer the question
In order to leave comments, you need to log in
If you do not go into the details of compilation, then you can, but there are limitations. If you want to shove a template implementation into a cpp file, then you need to instantiate it after the implementation so that the compiler can use it in other files (in which you include a declaration header). If you want to move the implementation to another header, then you need to include it in all cpp files where you want to use it.
Just to think. Each cpp file is compiled separately from the others, and let the implementation file be compiled first. How does it know for which template parameters it should be instantiated if it hasn't even worked with other files yet? Thus, when it comes to the file in which you use this template, the implementation you need most likely will not be. Therefore, together with the declaration, it is necessary to indicate for which parameters the template will be used. On average, this is not convenient, so they don’t do this and write templates in headers.
You can of course create an .inl file. They serve to implement inline functions or functions with template parameters.
But it's better to write the implementation in the header files.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question