I’m using the following pattern to keep template declarations and implementations separate:
decl.h (declaration)
template <typename T>
struct Foo
{
void DoIt();
}
impl.cpp (implementation)
template <typename T>
void Foo<T>::DoIt() { // impl code
};
template class Foo<int>;
template class Foo<float>;
Now i want to add a new method to Foo, but the impl.cpp file is already huge, so i want to move it to a separate file impl2.cpp;
decl.h (declaration)
template <typename T>
struct Foo
{
void DoIt();
void RemoveIt();
}
impl.cpp (implementation)
template <typename T>
void Foo<T>::DoIt() { // impl code
};
template class Foo<int>;
template class Foo<float>;
impl2.cpp (implementation)
template <typename T>
void Foo<T>::RemoveIt() { // impl code
};
template class Foo<int>;
template class Foo<float>;
the main concern here are the duplicate instantiations, how do i avoid those?
Do as follows:
Foo<T>::DoIt()to some header file namedImplDoIt.h (not necessarily in the same directory as decl.h)
Foo<T>::RemoveIt()– ImplRemoveIt.h