[cfe-dev] Typedef templates

John McCall rjmccall at apple.com
Thu Jun 21 10:41:31 PDT 2012


On Jun 21, 2012, at 8:23 AM, Lin, Alexander S. (JSC-ER711) wrote:
> I’m new to clang and this is my first post, so please forgive me for what may be a basic question.
> 
> I have a template like this:
> 
> template <class T, class U, class V>
> class MyTemplate {
>   public:
>     T var1 ;
>     U var2 ;
>     V var3 ;
> } ;
> 
> 
> And I have a typedef like this:
> 
> typedef MyTemplate< double , int , short > MyTemplateTypedef ;
> 
> What I’m trying to find is somewhere that stores the template with the arguments substituted in.  I have seen some of the template instantiating routines in Sema, but cannot figure out how to use them.  Am I on the right track here?

To quote from the TemplateSpecializationType docs:

  A non-dependent template specialization type is always "sugar", typically for a RecordType.

You can use getAs<RecordType>() to check whether it's a record type (it will be unless the template is a C++11 template alias), and then pull the RecordDecl out of that.  It will be a ClassTemplateSpecializationDecl (again, unless the template is a C++11 template alias that happens to resolve to a non-templated class).

Note that just writing a typedef of a template specialization type does not require (or even allow) instantiation of the template, so the ClassTemplateSpecializationDecl will not necessarily have a definition, and even if the class does, its member functions may not.

John.



More information about the cfe-dev mailing list