我正在研究一个示例代码,该代码试图讲授状态图的实现。我在typedef typename T::HostClass Host
等模板中看到typedef
为了清楚起见,模板定义为
template<typename T>
struct Init {
typedef typename T::HostClass Host;
Init(Host& h) :
host_(h) {
}
~Init() {
T::entry(host_);
T::init(host_);
}
Host& host_;
};
我知道
typename
旁边的typedef
的用途,并且正在解释Highly-Typed-Languages
(例如C++
)(伙计,我对吗?是吗?)困惑在
HostClass
用
typedef typename T::HostClass Host;
HostClass
? HostClass
? 最佳答案
HostClass
必须是嵌套类或T
范围内的另一个typedef(或别名类型)。
如果T
没有它,则代码将无法编译。