I am trying to write a special container in STL style. For the sake of simplicity, let's say a list. I looked at the standard way to define such a container:
template <typename T, typename A = std::allocator<T> > class mylist;
Now I want to manage list nodes using a nested class:
(inside mylist) class node { T data; node *next; }
As far as I understand, I do not need to specify the template specifier before the node definition, since the compiler will create separate classes mylist<T,A>::node for each combination of template parameters mylist .
However, now I need to allocate memory not only for data of type T , but also for their node wrapper. Thus, I would like the default template parameter to be of type std::allocator<mylist<T>::node> . At this point, however, mylist has not yet been declared, and the compiler is clearly upset:
error: `mylist' was not declared in this scope
How to solve this riddle? There are two limitations:
- I usually declare a missing class without fully declaring its contents. However, since it is nested inside the very thing that I want to declare, this is not an option.
- I need the
node be nested since it needs to access the instance of the mylist distributor. For example, I have operator= declared on node , where a lot of memory management happens recursively. This may be redundant for the list, and you can do it from mylist , thereby reducing the parametric dependence of node on A , but it is crucial for the data structure that I implement.
Jonas source share