Including .cpp at end of template header file
I was reading an older data-structures book and it said that when you're doing template class programming you should include the .cpp at the end of the .h file.
As far as I know you have to do full function implementations in your .h file for any template class member functions - it's due to the way the template compiler works.
The only functions I was taught could be put in a implementation file for a template class were template specialization functions i.e.: template<> Class<Type>::function_name()
.
Why would this book to suggest to include the .cpp at the end of the .h? is this just a way of separating the implementations into different files while getting them to compile with the header? and if so, where would you put real specializations - I'm guessing they couldn't go in the .cpp included 开发者_如何学Cby the header.
Most likely the author prefers having declaration and definition in different files, I can guess this is because it makes it easier to jump between declarations and definitions.
But having "cpp" file extension is a bit confusing. Usually those files are called "ipp" for "Inline C++".
This is probably old parlance, and I think your analysis of separation of implementation and declaration is correct. Back when this particular book was written, the author probably thought of a cpp file as the file where definitions resided, and h files as the files where declarations resided. Putting real explicit specializations into the former file would, naturally, be lethal (or at least useless) to the linker because of repeated definitions. Nowadays, I'd avoid naming the definitions file .cpp.
First, there's no rule that you have to have the implementations of the
templates in your .h
(or .hpp
, or .hh
) file; in fact, for anything
but the simplest templates, I would recommend against doing so. You do
have to include the implementation, regardless of the file. What the
author probably had in mind was to put the implementation in a .cpp
file, and include that. I'd recommend finding a different name,
however, since most people (and some IDE's) will suppose that you should
compile all .cpp
files. A common convention where .cc
and .hh
are
the usual extensions for sources and headers is .tcc
for template
implementations; in a Windows world (where .cpp
is almost universal),
I'd recommend something like .tpp
.
Note that the earliest implementations of templates required the
implementation to be in a .cpp
. These implementations didn't require
(or allow) it to be included, however; the compiler searched for the
.cpp
(or .cc
) corresponding to the .hpp
(or .hh
) file in which
the template class definition or function declaration appeared, and
generated a dummy source file which included it (and anything else which
was necessary).
As far as I know you have to do full function implementations in your .h file for any template class member functions - it's due to the way the template compiler works.
May be the book assumed you have the full function implementations in your .cpp
file.
If full specializations are in cpp file, they should not be included in header file. Most likely it wont compile if you did so. Because compiler will see multiple definitions of the same function as header file will generally be included in multiple source files.
If you want to use a template, the entire template definition has to be visible in the TU that instantiates the template. So you could simply put full definitions in the header file.
It's only if you really want to keep the class definition and class member function bodies separate, then you might be tempted to put those function definitions into a separate file. However, the above rule still applies, so you'll have to include the separate file together with the header in order for anyone to be able to use the template.
It's a matter of taste. If the member function bodies are short, you might as well define them inline. Also bear in mind that functions that are defined inside the class definition are implicitly declared inline
, while you'd have to specify that explicitly if you write the bodies separately.
A partially specialised templated is still a template, so the same rules apply.
A fully specialized class template is just an ordinary class, so you treat is as such:
// FooInt.hpp
template <typename> class Foo;
template <> class Foo<int>
{
// your class here
};
One thing you may have had in mind is "explicit template instantiation", a la template class Foo<int>;
. That's something else; leave a comment if you're interested in using that.
精彩评论