The only portable way of using templates at the moment is to implement them in header files by using inline functions.
What does this sentence mean?
The only portable way of using templates at the moment is to implement them in header files by using inline functions.
What does this sentence mean?
Although Standard C++ has no such requirement, some compilers require that all function and class templates need to be made available in every translation unit they are used in. In effect, for those compilers, the bodies of template functions must be made available in a header file. To repeat: that means those compilers won't allow them to be defined in non-header files such as .cpp files
There is an export keyword which is supposed to migitate this problem, but it's nowhere close to being portable.
Templates need to be instantiated by the compiler before actually compiling them into object code. This instantiation can only be achieved if the template arguments are known. Now imagine a scenario where a tempate function is declared in a.h
, defined in a.cpp
and used in b.cpp
. When a.cpp
is compiled, it is not neccessarily known that the upcoming compilation b.cpp
will require an instance of the template, let alone which specific instance would that be. For more header and source files, the situation can quickly get more complicated.
One can argue that compiers can be made smarter to "look ahead" for all uses of the template, but I'm sure that it wouldn't be difficult to create recursive or otherwise complicated scenarios. AFAIK, compliers don't do such look aheads. As Anton pointed out, some compilers support explicit export decarations of tempate instantiations, but not all compilers support it (yet?).
HTH
Because when instantiating a template, the compiler creates a new class with the template parameter. For example:
template<typename T>
struct Foo
{
T bar;
void doSomething(T param) { // do stuff using T }
};
// somewhere in a .cpp
Foo<int> f;
When reading this line, the compiler will create a new class (let's call it FooInt), which is equivalent to this one:
struct FooInt
{
int bar;
void doSomething(int param) { // do stuff using int }
}
Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template parameter (in this case int). If these implementation was not in the header, they wouldn't be accessible, and therefore the compiler wouldn't be able to instantiate the template.
A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.
// Foo.h
template <typename T>
struct Foo
{
void doSomething(T param);
};
#include "Foo.tpp"
// Foo.tpp
template <typename T>
void Foo<T>::doSomething(T param)
{
//implementation
}
This way, implementation is still separated from declaration, but is accessible to the compiler. Another solution is to keep the implementation in a .cpp, and explicitly instantiate all the template instances you'll need, in the header:
// Foo.h
template <typename T> struct Foo { ... };
template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int of float
If my explanation isn't clear enough, you can have a look at the C++ FaqLite on this subject.
It means that the most portable way to define method implementations of template classes is to define them inside the template class definition.
template < typename ... >
class MyClass
{
int myMethod()
{
// Not just declaration. Add method implementation here
}
};
Actually, the C++ standard defines the 'export' keyword that would make it possible to simply declare templates in a header file and implement them elsewhere.
Unfortunately, none of the popular compilers implements this keyword. The only one I know about is the frontend written by the Edison Design Group, which is used by the Comeau C++ compiler. All others are stuck with having to write templates in header files, because the compiler needs the definition of the code for proper instantiation (as others pointed out already).
Plenty correct answers here, but I wanted to add this (for completeness):
If you, at the bottom of the implementation cpp file, do explicit instantiation of all the types the template will be used with, the linker will be able to find them as usual.