www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Caching of Template Instantiations

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Does DMD cache template instantiations?

That is, is it preferred to do, for instance, either

     static      if (isIntegral!T && isUnsigned!(T)) {}
     else static if (isIntegral!T && isSigned!(T)) {}

or

     enum integral = isIntegral!T;
     static      if (integral && isUnsigned!(T)) {}
     else static if (integral && isSigned!(T)) {}
Oct 17 2015
parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Saturday, 17 October 2015 at 07:48:39 UTC, Nordlöw wrote:
 Does DMD cache template instantiations?
Yes, and it's required by the spec: "Multiple instantiations of a TemplateDeclaration with the same TemplateArgumentList all will refer to the same instantiation." http://dlang.org/template.html
 That is, is it preferred to do, for instance, either

     static      if (isIntegral!T && isUnsigned!(T)) {}
     else static if (isIntegral!T && isSigned!(T)) {}

 or

     enum integral = isIntegral!T;
     static      if (integral && isUnsigned!(T)) {}
     else static if (integral && isSigned!(T)) {}
It *might* make a little difference if you have thousands of instantiations, because for each instantiation the compiler needs to check whether it has already instantiated the templation with that combination of arguments, but I wouldn't worry about it.
Oct 17 2015