我想以不同的方式处理模板参数,因此对于代码:
template <class T> class A {
public:
A() {}
};
void faa(A<int>& param);我想知道param是一个模板专门化,并获得访问它的参数。
所以我写了一个带有函数的ASTVisitor
bool VisitFunctionDecl(FunctionDecl *f) {
std::cout<< "VisitFunctionDecl" <<std::endl;
const DependentTemplateSpecializationType* t1;
const TemplateSpecializationType* t2;
for(ParmVarDecl* p :f->params())
{
t1=p->getType()->getAs<DependentTemplateSpecializationType>();
t2=p->getType()->getAs<TemplateSpecializationType>();
if(t1!=nullptr||t2!=nullptr)
{
std::cout<< "template param found" <<std::endl;
}
}
return true;
}但这些转换都是nullptr始终都是--我从来没有得到template param found输出。
我做错了什么?是否有其他方法可以将t转换为某种类型的国王,允许检查模板参数?
发布于 2015-08-16 15:19:03
A<int>&的类型是LValueReference (可以用getTypeClassName()检查)。您可能想得到的是引用所指向的类型。您可以使用 方法获得它。
bool VisitFunctionDecl(FunctionDecl *f) {
llvm::errs() << "VisitFunctionDecl:" << f->getQualifiedNameAsString()
<< "\n";
for (ParmVarDecl* p : f->params()) {
llvm::errs() << p->getType().getAsString() << " -> "
<< p->getType()->getTypeClassName() << "\n";
llvm::errs() << "isPointerType: "
<< p->getType()->hasPointerRepresentation() << "\n"
<< "isTemplateSpecialization: "
<< (nullptr != p->getType().getNonReferenceType()->getAs<
TemplateSpecializationType>()) << "\n";
}
return true;
}产出如下:
VisitFunctionDecl:faa
const A<int> & -> LValueReference
isPointerType: 1
isTemplateSpecialization: 1https://stackoverflow.com/questions/31861623
复制相似问题