Last update February 4, 2008

Template Instantiation



Note:Information in this article is outdated. D now has a implicit instantiation when templates have one member with the same name.

The topic of template instantiation has come up many times on the D newsgroup. But the topic of template instantiation doesn't come up nearly as often since implicit function template instantiation ( IFTI) was added.

For information on templates visit: Templates Templates Revisited

Background

It used to be described in the D specification that templates must be explicitly instantiated such as

TFoo!(int) abc;
This syntax was chosen to prevent ambiguous code. The problem was that explicit instantiation made templates much more cumbersome in D than its C++ cousin.

Why D Templates Didn't Have Implicit Instantiation

From NG:digitalmars.D/7704

 For a long time I felt that implicit function instantiation of templates was
 where C++ templates ran off the road into the weeds. Certainly, I still feel
 that way about overloading them with regular functions of the same name, but
 I'm not so sure about the rest. I still loathe things like implicitly
 instantiating template functions by taking their address, mainly because it
 is a b*tch to get to work right in a language that fundamentally does bottom
 up typing (this kind of instantiation is top down, and so where they meet is
 a swamp of bugs).

Implict instantiation is an enormous increase in complexity.

-- Walter

Suggestion

Use another syntax for instantiation

  • Name<ARG>, NG:D/19594 by Heretic
    • Criticism: NG:D/19603 by Andy Friesen
      The trouble with Name<ARG> is (as I understand it) that it requires 
      semantic analysis to figure out whether < and > are being used as angle 
      brackets, or as greater-than/less-than in an expression.
      
      Plain-old parenths might cut it, but that makes what's going on less 
      obvious.  Braces already have meaning as well.
      
      I agree that template instantiation should be much less verbose; it's 
      just a matter of working out a good syntax for it.
      
        -- andy
    • Criticism: NG:D/19604 by Ben Hinkle
       Plus in C++ "foo<bar<T>>" needs to be "foo<bar<T> >" with a space between
      the last two >'s since >> is the right-shift operator. This is a nasty
      gotcha.

FolderDiscussions

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: February 4, 2008 17:22 (diff))