1

我想以不同的方式处理模板参数,因此对于代码:

template <class T> class A { 
public:
     A() {} 
};

void faa(A<int>& param);

我想知道 param 是模板专业化并可以访问它的参数。

所以我写了一个ASTVisitorwith 函数

 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 转换为允许检查模板参数的类型之王?

4

1 回答 1

1

的类型A<int>&LValueReference(可以用 来检查getTypeClassName())。您可能想要得到的是引用指向的类型。你可以通过getNonReferenceType() 方法得到它。

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: 1
于 2015-08-16T15:19:03.373 回答