5
votes

I do know that template class's definition and implementation should be on the same header file. But I was taught a bit differently at school.

I'll have the template class's definition in the header file, and at the end of the header file, I'll do #include "MyFile.cpp", which contains the implementation of the templated class.

Is this bad programming practice?

1
Nope, it's very common for implementations to do this although they tend to give a different suffix to make it clear it's not meant to be compiled separately like "tcc" in the case of GCC. - user657267
It's just a convention issue. - songyuanyao
Thanks for the replies guys! - dwnenr
You just have to make sure that whatever build system you're using doesn't automatically try to compile the .cpp file. And include it within the include guards. - juanchopanza
It's a pointless slight of hand in my view. A cpp (or tcc) file included in a header is a header for all intents and purposes. Might as well do the straightforward thing and put all the code in a single file. - john

1 Answers

5
votes

"Is this bad programming practice?"

In general not and it's a very common technique. But the problem is the .cpp file extension, that would affect many IDEs and build systems to consider it as a regular source file. More commonly used extensions are .tcc, .tpc.