Why use base classes for list/tree nodes in the C++ standard library?

Viewed 138

In libstdc++ and libc++ the internal nodes for lists (e.g. list and forward_list) and other trees are constructed in (at least) two parts: a node base class; and the node class itself. For example, rather than:

struct sl_full_node {
  sl_full_node *p;
  int value;
};

...we would see something like:

struct sl_node_base {
  sl_node_base *p;
};

struct sl_node : sl_node_base {
  int value;
}

A simple, self-contained example of this idiom can be found in the singly linked list proposal from 2008 here. What are the benefits of this approach?

0 Answers
Related