In regular C++ classes, we can have a class A as follows:
in A.h:
class A {
public:
void method();
// rest of class declaration
};
in A.cpp:
#include "A.h"
#include "implementation_dependencies.h"
void A::method(){
// use dependencies to implement method
}
But if A is a template class, this isn't possible. How can we still achieve information hiding with C++ template classes?
The use of extern template in C++11 (or global function prior to C++11 with the same effect) proves useful for this, but what to do when the template class needs to be available for all types?
Like a smart pointer for example: I can't put the definition of the class inside A.h since that would expose "implementation_dependencies.h" to anyone including "A.h".
From c++20, you can do this with modules:
// A.cpp
export module A;
export
template <typename>
class A {
// ...
};
#include "implementation_dependencies.h"
export template <typename T>
void A<T>::method() {
// use dependencies, but don't export them from this module
}