Think of templates as code generation. If you do not know in advance which template you will use, you will not be able to compile. Therefore, you need to save the implementation in the header.
This allows you to make some investments, and this explains why sometimes using templates (like std :: sort) works faster than in regular C.
source share