In Stroustrup's C++ Programming Language (4th ed), section 27.4.2 shows a technique to "linearize" a diamond class hierarchy to avoid the overhead of virtual base classes. He starts with the diamond pattern from a real project (Pivot code analyzer tool):
The linear version is drawn as:
and
The code outline is:
namespace ipr {
struct Node { ... };
struct Expr : Node { ... };
struct Stmt : Expr { ... };
struct Decl : Stmt { ... };
struct Var : Decl { ... };
namespace impl {
template<class T> struct Node : T { ... };
template<class T> struct Expr : Node<T> { ... };
template<class S> struct Stmt : S { ... };
template<class D> struct Decl : Stmt<Expr<D>> { ... };
struct Var : Decl<ipr::Var> { ... };
}
}
I'm confused by the irregular structure. Based on the initial description, I was expecting impl
to look something like:
namespace impl {
template<class T> struct Node : T { ... };
template<class T> struct Expr : Node<T> { ... };
template<class S> struct Stmt : Expr<S> { ... };
template<class D> struct Decl : Stmt<D> { ... };
struct Var : Decl<ipr::Var> { ... };
}
And I'm thinking the full diagram of these classes is:
My question is, why don't the "interior" three impl
template classes have parallel forms, as in my version of the code?