I have a nested class definition that I wanted to pass down as the parent class (class containing the nested class, not the class that's being inherited) template parameter. Since the template does not seem to be aware of the nested class's existance, I tried to pass it down as an incomplete type, only to read later that doing so is usually a bad idea and is only rarely permitted (such as in the case of shared_ptr
).
I know this can be very easily solved by simply declaring the nested class externally (which is what I do) but I am asking this because I want to learn if there is any way of achieving the same effect because I an fond of how they do not pollute the namespace how they are "associated" with the parent class definition without exposing anything.
Here's an example to perhaps make it more clear on what I am talking about:
#include <memory>
using namespace std;
template <class T> class shared_class {
protected:
shared_ptr<T> d = make_shared<T>();
T* container() { return d.get(); }
};
// A is the "parent" class.
class A : public shared_class<C> { // Compiler error: C is undefined.
// Similarly, A::C will complain that no C is in A.
class C { // Nested class
public:
int i = 0;
};
};
Is there another way of doing this with templates in a way that the entire definition of the nested class is contained entirely within the parent class definition?