This question has already been answered:

Is it possible to implement a template in cpp and not in the h file?

Reported as a duplicate by participants user194374, Harry c ++ Jan 29 '17 at 8:01 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    In general, you can implement a template in any desired file, but remember that [parameterized] definitions of functions and template classes should be visible wherever they are used (specialized, instantiated).

    If you use your template in only one translation unit (in one .cpp file) and nowhere else, then please - implement it in this .cpp file for health.

    And if your template is used in several different translation units, then your .cpp file will still have to be included via #include wherever it is needed, exactly as is done with the .h files.

    For this reason, traditionally, the entire implementation of the template class, including its method definitions, is placed in the .h file. In the .cpp file there are only definitions of explicitly specialized methods (explicit specialization), if any.

    (That is, the answer is the question that you actually tried to ask - no, you can not.)

    • So to say, alaverdi :) If such an implementation is in .cpp, and this file is included in different project files, then it will be impossible to do anything in it except what is possible in the header file ... So such an included .cpp file is essentially an included h-file. The functionality, so to speak, of files — the header, the implementation — is not determined by the extension , but by the way it is used . - Harry