Consider these classes:
Instance.h:
template<typename T>
class Instance {
public:
string getName() const {
return instance->getName();
}
private:
T instance;
}
Layer.h:
#include "Instance.h"
class Layer {
public:
void method() {
instance.getName();
}
string getName() const {
return "";
}
private:
Instance<shared_ptr<Layer>> instance;
}
Obviously some pseudocode above, but something like this fails to compile in Instance.h with “use of undefined type ‘Layer'”. I gather it’s because we have an Instance<shared_ptr<Layer>>
inside Layer, it isn’t fully constructed by the time it needs to be used within Instance.h (?)
I can resolve it by putting the getName() implementation inside an Instance.cpp file and this at the bottom:
template class Instance<shared_ptr<Layer>>;
However this sort of defeats the purpose of the template as now I will have to include specializations for each type where this happens.
Is this the only solution (short of removing the Instance reference within Layer), or is there some other answer that doesn’t require the specialization?
Thank you!
1