[cfe-users] How to determine if variable is templated?

David Blaikie via cfe-users cfe-users at lists.llvm.org
Thu Feb 20 16:30:23 PST 2020

I don't understand - you'll need to be more specific. (anything that's
"this doesn't /always/ happen" sounds like there's a /lot/ going on that's
not specified in your question... compilers in general and clang in
particular are /very/ deterministic, so they do do the same thing every
time - so if there's some variation it's likely because you're looking at
different things)

If you're looking at a particular instantiation of the variable template
tVar, well, once you ask for the type of that particular instantiation
you'll be looking at a concrete type and probably too late to find out
anything about the template-y-ness.

But if you are actually looking at the DeclRef for the variable template
instantiation, then you can get the VarDecl from that DeclRef and then you
can query the VarDecl to see if it's a template as I mentioned in the link
previously - "getTemplateSpecializationKind" on the VarDecl should tell you
if it's a template at all.

At least I think that's how you'd go about it...

- Dave

On Sun, Feb 20, 2020 at 4:08 PM Robert Ankeney via cfe-users <
cfe-users at lists.llvm.org> wrote:

> David - thanks for your suggestions, but no luck here. I had hoped for
> something like a isTemplateTypeParam() function in QualType, but no such
> luck. In many cases, if I do a getCanonicalType().getAsString() I get a
> "type-parameter-0-0" as a result. Though not always. I don't even always
> see "Type" returned if I do a getUnqualifiedType().getAsString(). I had
> hoped for a simple function call, as these template parameters seem like
> generic types.
> Robert

On Mon, Feb 17, 2020 at 9:28 AM David Blaikie <dblaikie at gmail.com> wrote:
Guessing these sort of functions would be relevant:
>> https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html#a617bcdd5baaab0ccba71a96cd9c2ed03
> On Sun, Feb 16, 2020 at 5:43 PM Robert Ankeney via cfe-users <
>> cfe-users at lists.llvm.org> wrote:
>>> Suppose I have some code like:
>>> template<typename Type>
>>> Type tVar;
>>> function(tVar);
>>> How can I determine that tVar is templated from the VarDecl or a
>>> MemberExpr? I don't see any obvious function in QualType or Type.
>>> Thanks,
>>> Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20200220/43c5f7f5/attachment.html>

More information about the cfe-users mailing list