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 に答える