c++ - 在模板中使用与模板类相同类型的类型别名它自己
问题描述
我不明白singly_linked.hpp中的一些代码:
template <class T>
struct singly_linked {
// -- member types -----------------------------------------------------------
/// The type for dummy nodes in singly linked lists.
using node_type = singly_linked<T>; // exactly type of node_type???
/// Type of the pointer connecting two singly linked nodes.
using node_pointer = node_type*;
// -- constructors, destructors, and assignment operators --------------------
singly_linked(node_pointer n = nullptr) : next(n) {
// nop
}
// -- member variables -------------------------------------------------------
/// Intrusive pointer to the next element.
node_pointer next;
};
究竟是什么类型node_type
?它会导致无限循环吗?例如,如果我有:
singly_linked<int> node;
那么是什么类型的singly_linked<int>::node_type
呢?
解决方案
你误解了 的意思using node_type = singly_linked<T>;
。它没有声明类型变量singly_linked<T>
(这确实会导致无限递归并导致编译器错误)。相反,这为这种类型引入了一个别名:singly_linked<T>
.
因此,询问类型是singly_linked<int>::node_type
没有意义的,因为它本身就是一种类型。
推荐阅读
- jquery - 如何将 $.ajax 与 then() 一起使用?
- javascript - 如何修复自定义警报功能
- vue.js - WebStorm Vue 未解析的变量或类型
- facebook-android-sdk - Facebook 原生广告未显示,错误“1011 - 显示格式不匹配”
- angular - 从命令行自动将新生成的组件添加到 entryComponents
- reactjs - 我正在尝试从 redux-Form 迁移到 react-Final-Form
- javascript - 更改使用 amcharts 绘制的 3D 条形图标签的字体颜色和标题
- listview - Flutter - 如何覆盖滚动物理
- unix - 特定场景的 Unix 命令
- ios - 有没有办法将 CSR 解码为字符串以获取属性?