[cfe-dev] Adding template default parameter with as typed explicit parameter

Eli Friedman eli.friedman at gmail.com
Thu Sep 27 16:07:05 PDT 2012


On Thu, Sep 27, 2012 at 6:47 AM, Philippe Canal <pcanal at fnal.gov> wrote:
> Hi,
>
>
>> The CXXRecordDecl has to use the canonical types so we don't try to
>> instantiate Point twice if the user writes e.g. "Point<size_t> x;
>> Point<unsigned long> y;".
>
>
> Yes, indeed.  I do not want to change that.
>
>
>> I suppose it could store non-canonical
>> arguments corresponding to the first instantiation as well... but we
>> haven't had any use for that in the past.
>
>
> In our use case there wouldn't be anything magic about the first
> instantiation.
>
>
>> AFAIK no... I'm pretty sure Sema never computes it
>
>
> Yes.  However, in the case of auto describing data persistency, one
> would want to preserve not only the type as written by the user
> (Point<size_t> in my example) but also the default value of the template.
> This would help in schema evolution (i.e. being able to read the old
> data with a newer schema/library where the default value of the template
> may have changed).
>
> From a QualType instanceType (gotten from the variable declaration for
> example)
> which represent Point<size_t>
>
>    const clang::TemplateSpecializationType* TST
>       = llvm::dyn_cast<const
> clang::TemplateSpecializationType>(instanceType.getTypePtr());
>
> I can get the arguments of the template as types (size_t).
>
> I think we need to keep that.   What I would also like to have (and am
> offering to implement but would need directions on where best to do it
> in the code), is something akin to
>
>   TST->getTypeWithDefaultExpanded();
>
> which would return a QualType/TemplateSpecializationType (but not a
> CXXRecordDecl) representing
>    Point<size_t, Coord<size_t> >'
>
> which would still point to the CXXRecordDecl for Point<long, Coord<long> >.

Hmm... yes, that's possible, but I'm not sure we'd want to pay the
price in memory usage/compile-time for normal compiles (IIRC, the
relevant code is in semantic analysis, so we would have to pre-compute
it then).

-Eli



More information about the cfe-dev mailing list