I was going through EASTL’s list class to look at how the author has implemented the nodes. My expectation was a simplistic class/structure. Instead, I see a base and a node that is inheriting from this base (still simplistic, but why two classes?). His comments explain why:
We define a ListNodeBase separately from ListNode (below), because it allows
us to have non-templated operations such as insert, remove (below), and it
makes it so that the list anchor node doesn’t carry a T with it, which would
waste space and possibly lead to surprising the user due to extra Ts existing
that the user didn’t explicitly create. The downside to all of this is that
it makes debug viewing of a list harder, given that the node pointers are of
type ListNodeBase and not ListNode. However, see ListNodeBaseProxy below.
I don’t understand a couple of things here. I do understand the part about why it will make debug viewing a bit harder, but what does he mean by list anchor node doesn't carry a T with it and would waste space and possibly lead to surprising the user due to extra Ts existing that the user didn't explicitly create?
Without the helper class, the list root node would contain an instance of T that is never used. The second sentence is saying that you might not expect an empty list to create a T. For example, creating a T might have side effects.