c++ - 这个模板有什么作用?
问题描述
在 llvm 源代码llvm/IR/PassManager.h
中有这个模板(为简洁起见,已删除注释):
struct alignas(8) AnalysisKey {};
struct alignas(8) AnalysisSetKey {};
template <typename IRUnitT> class AllAnalysesOn {
public:
static AnalysisSetKey *ID() { return &SetKey; }
private:
static AnalysisSetKey SetKey;
};
template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
编辑:我只对此感到困惑:template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
我熟悉为类或函数使用模板。上面的用法看起来很不一样。模板定义后面不是类或函数,而是结构体和类成员变量。任何对它的作用或为什么以这种方式编写的洞察力都将不胜感激。
解决方案
SetKey
是一个静态数据成员,所以在这一行
template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
您正在提供该变量的定义,以便在使用此模板时可以将内存分配给静态变量。
如果您使用的是 C++17,则此行可以写为:
static inline AnalysisSetKey SetKey;
然后你确实需要写下你很难理解的那一行。
推荐阅读
- python - 如何使用 selenium 在 Python 中测试 CSS 值?
- nuxt.js - 如何在导航栏后面放置背景图片?
- content-security-policy - 内容安全策略阻止明确列出的主机违反 script-src-elem
- css - CSS box-shadow 过渡与悬停到 inset-box 阴影
- javascript - 当数据点在html中是动态的时如何从网站上抓取和保存图表
- android - 在本机反应当我运行 npm run android 命令时,我得到了这个错误。在 android 文件夹中我找不到 gradlew.bat 文件
- sql - 根据某些过滤条件从雪花数据库中的表中删除重复项
- controller - exo_player_control_view 重叠缓冲(进度条)
- jquery - 通过 JavaScript jQuery 与图像的动态纵横比
- python-3.x - 有没有办法删除嵌套列表中的元素?