How can I get the template argument in a TypedefDecl ?


For the following code:
class A {

class B {};

typedef A C;

I need to access this ‘B’ template argument, as a Type pointer, maybe.

(Ultimately, I only need its name as a string)

Can anyone point me to the right way to get this information ?

I can get a QualType for A with the getTypeSourceInfo()->getType()


But what to do after that, I can not figure out.

Manasij Mukherjee


Try this:

std::string getTypeName(QualType QT) {

std::string TypeName;
llvm::raw_string_ostream TypeNameOS(TypeName);
LangOptions LO;
QT.print(TypeNameOS, PrintingPolicy(LO), “identifier”);
return TypeName;


That gives me “A identifier”.

(which could also be achieved with QT.getAsString(), afaik)

I have the QualType for A.

But I have to get “B” from this somehow.

Sorry if I was ambiguous about that.

I think you need to cast QT.getTypePtr() to a TemplateSpecializationType and then process its TemplateArguments.

Thanks, that seems to work.

Code for someone stumbling into this question later:

if (const TemplateSpecializationType* tst
= dyn_cast(D->getTypeSourceInfo()->getType().getTypePtr())){
for (uint i = 0; i < tst->getNumArgs(); ++i ) {
const TemplateArgument& arg = tst->getArg(i);
if (arg.getKind() == TemplateArgument::ArgKind::Type)
std::string foo = arg.getAsType().getAsString();