clang中classof方法的目的是什么?
What is the purpose of method classof in clang?
让我给你一个例子来解释我想做什么(或者至少知道这是可能做到的):
在Clang中,让我们取一些基本的ValueDecl。从提供的链接中可以看到,这个ValueDecl
可以:
- 是一个简单的
ValueDecl
- 做一个DeclaratorDecl
- 为EnumConstantDecl
- 成为一个间接字段decl
- 为UnresolvedValueDecl
我想知道,给定一个ValueDecl *
,我是否可以确定它是否是上面列出的类之一,或者我是否被限制在这个ValueDecl *
?
在每个类中,都有这个bool classof()方法,但是我不明白这个方法的目的。这能解决我的问题吗
classof
确实是解决方案的一部分,但通常不意味着直接使用。
相反,你应该使用isa<>
, cast<>
和dyn_cast<>
模板。LLVM程序员手册中的一个例子:
static bool isLoopInvariant(const Value *V, const Loop *L) {
if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V))
return true;
// Otherwise, it must be an instruction...
return !L->contains(cast<Instruction>(V)->getParent());
}
cast<>
和dyn_cast<>
的区别在于前者断言实例是否不能强制转换,而前者仅仅返回一个空指针。
注意:cast<>
和dyn_cast<>
不允许空参数,如果参数可能为空,可以使用cast_or_null<>
和dyn_cast_or_null<>
。
要进一步了解没有virtual
方法的多态性设计,请参阅LLVM isa<>
是如何实现的,您将注意到它在幕后使用classof
。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 通过JNI传递数据数组的最快方法是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在C++中包含原型文件的正确方法是什么?
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 将一系列整数放入类的最佳方法是什么?
- 从长整整转换为uint64_t的推荐方法是什么?
- C++:使用方法调用析构函数的顺序是什么?
- 将此布尔值传递给此函数的最有效方法是什么?