Stroustrup线性化类层次结构示例的说明 < Stroustrup的C++编程语言(第四ED),第27.4.2节展示了一种“线性化”钻石类层次结构以避免虚拟基类的开销的技术。他从一个真实项目()的钻石图案开始:

Stroustrup线性化类层次结构示例的说明 < Stroustrup的C++编程语言(第四ED),第27.4.2节展示了一种“线性化”钻石类层次结构以避免虚拟基类的开销的技术。他从一个真实项目()的钻石图案开始:,c++,diamond-problem,C++,Diamond Problem,线性版本绘制为: 及 守则大纲如下: namespace ipr { struct Node { ... }; struct Expr : Node { ... }; struct Stmt : Expr { ... }; struct Decl : Stmt { ... }; struct Var : Decl { ... }; namespace impl { template<class T> struct

线性版本绘制为:

守则大纲如下:

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> { ... };
    }
}
我认为这些类的完整图表是:


我的问题是,“内部”三个
impl
模板类为什么不像我的代码版本那样具有并行形式?

我最好的猜测来自于查看

template<class D> struct Decl : Stmt<Node<D>> { ... };
我不知道为什么斯特劳斯塔普没有解释这种不正常现象。也许他认为他是通过将
Stmt
更改为
Stmt
来删除它的,或者他根本没有更改它(也就是说,代码在复制后更改了),他想保持原样,而不解释每一个细节

希望能有更好的答案来解释这一点

template<class D> struct Decl : Stmt<Node<D>> { ... };
template<class D> struct Decl : Stmt<Expr<D>> { ... };
struct For : Stmt<Expr<ipr::For> > { ... }