Reputation: 22113
I want to create a template class (let's call it Foo
) that only accepts a few specific type parameters (let's say only double
and float
). Normally templates are implemented in the header file (.h
) because it is unknown how it will be instantiated in user code. In this case, it makes more sense to implement the class in the implementation file (.cpp
) like so:
// Foo.cpp:
template <class T>
class Foo
{
// Insert members here
};
typedef Foo<double> Foo_d;
typedef Foo<float> Foo_f;
This would instantiate and compile the class when Foo.cpp is compiled. But then how to I declare this in the header file without writing separate declarations for Foo_d
and Foo_f
?
Upvotes: 3
Views: 1069
Reputation: 70392
You can define the template in your header file, declaring the methods, but without defining them. For example:
template <typename T>
class Foo {
T val;
public:
Foo (T t);
T value ();
};
typedef Foo<double> Foo_d;
typedef Foo<float> Foo_f;
In the .cpp
file, you complete the implementations of the methods, and then instantiate the templates that you want.
#include "foo_template.hpp"
template <typename T>
Foo<T>::Foo (T t) : val(t) {}
template <typename T>
T Foo<T>::value () { return val; }
template class Foo<double>;
template class Foo<float>;
The object file should have instantiations for Foo_d
and Foo_f
from the explicit instantiations of template Foo
. Any other type used for template Foo
will result in a linking error, since instantiations for them won't exist. Or, more pedantically, the compiler creates instantiations on demand as usual, but it won't be able to resolve the symbols corresponding to the methods of the class, because explicit instantiations for those won't exist.
Upvotes: 6
Reputation: 372724
Unless I'm mistaken, what you're describing is exactly the use case for the new C++11 extern template
feature. To use this feature, you would put the interface for the template class in the header file. You would then end the header file with these lines:
extern template class Foo<float>;
extern template class Foo<double>;
This tells any file that includes the header file not to try to instantiate the template when using it. Then, in your C++ file, you would then implement the template class, and then would end with the lines
template class Foo<float>;
template class Foo<double>;
These last two lines force the compiler to instantiate the template in this translation unit, so you won't get any linker errors.
Hope this helps!
Upvotes: 5