www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why Templates?

reply "Derek Parnell" <derek psych.ward> writes:
I'm sure its because I'm not from a C++ background, but I just don't get  
templates. What is the problem that Templates solve?

 From my limited experience they just seem to be a shorthand for a way of  
avoiding coding identical algorithms that only vary in the datatypes that  
are passed to them. Is that it or am I missing some fundamental elements?

And is that why people are clamoring for implicit template instantation -  
to further reduce the amount of typing to do (and remembering to  
instantiate the only the required renditions of the algorithm).

-- 
Derek Parnell
Melbourne, Australia
Mar 04 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Derek Parnell wrote:
 I'm sure its because I'm not from a C++ background, but I just don't get 
 templates. What is the problem that Templates solve?
 
  From my limited experience they just seem to be a shorthand for a way 
 of avoiding coding identical algorithms that only vary in the datatypes 
 that are passed to them. Is that it or am I missing some fundamental 
 elements?

That was the original intent. As far as I know, the original idea came from Alexander Stepanov (http://www.stepanovpapers.com/), who is also responsible for generics in Ada. But since their introduction, templates have also shown to be a useful high-performance alternative to run-time polymorphism, not to mention the more advanced code-generation trickery that has become so popular in the C++ world.
 And is that why people are clamoring for implicit template instantation 
 - to further reduce the amount of typing to do (and remembering to 
 instantiate the only the required renditions of the algorithm).

For the most part, yes, as template names can become quite long, and calling functions as: fn!(typeof(a),typeof(b))(a,b); is a bit annoying and reduces readability. ITI is also quite useful in C++ with respect to how overload resolution is handled, but I have no idea how this will translate to the far simpler overloading rules in D. I think we really may have to simply want and see what Walter comes up with for ITI in D, as simply eliminating the need to explicitly specify template parameters doesn't encompass the full utility of ITI in C++. Will we be able to overload template functions with each other? Will they also overload with non-template functions? What happens if there are multiple matches? class C(T) {} template func(T) { void func( T val ) {} } template func(T) { void func( C!(T) val ) {} } void func( C!(int) val ) {} func( new C!(int) ); In C++, this is legal and the result is clearly defined (the "most specialized" overload is called), but how will this translate to D? Will it even be legal? Sean
Mar 04 2006
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:duda9o$137k$1 digitaldaemon.com...
 is a bit annoying and reduces readability.  ITI is also quite useful in 
 C++ with respect to how overload resolution is handled, but I have no idea 
 how this will translate to the far simpler overloading rules in D. I think 
 we really may have to simply want and see what Walter comes up with for 
 ITI in D, as simply eliminating the need to explicitly specify template 
 parameters doesn't encompass the full utility of ITI in C++. Will we be 
 able to overload template functions with each other?

Yes.
 Will they also overload with non-template functions?

No. This is a misfeature in C++, and the equivalent can be done with explicitly specialized template functions.
 What happens if there are multiple matches?

     class C(T) {}

     template func(T) { void func( T val ) {} }
     template func(T) { void func( C!(T) val ) {} }
     void func( C!(int) val ) {}

     func( new C!(int) );

 In C++, this is legal and the result is clearly defined (the "most 
 specialized" overload is called), but how will this translate to D?

It'll be the most specialized.
 Will it even be legal?


 Sean 

Mar 04 2006
parent Sean Kelly <sean f4.ca> writes:
Walter Bright wrote:
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:duda9o$137k$1 digitaldaemon.com...
 is a bit annoying and reduces readability.  ITI is also quite useful in 
 C++ with respect to how overload resolution is handled, but I have no idea 
 how this will translate to the far simpler overloading rules in D. I think 
 we really may have to simply want and see what Walter comes up with for 
 ITI in D, as simply eliminating the need to explicitly specify template 
 parameters doesn't encompass the full utility of ITI in C++. Will we be 
 able to overload template functions with each other?

Yes.
 Will they also overload with non-template functions?

No. This is a misfeature in C++, and the equivalent can be done with explicitly specialized template functions.

Cool. I feel pretty much the same.
 What happens if there are multiple matches?

     class C(T) {}

     template func(T) { void func( T val ) {} }
     template func(T) { void func( C!(T) val ) {} }
     void func( C!(int) val ) {}

     func( new C!(int) );

 In C++, this is legal and the result is clearly defined (the "most 
 specialized" overload is called), but how will this translate to D?

It'll be the most specialized.

Very nice :-) Yet more to look forward to! Sean
Mar 04 2006
prev sibling next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Derek Parnell wrote:
 I'm sure its because I'm not from a C++ background, but I just don't 
 get  templates. What is the problem that Templates solve?
 
  From my limited experience they just seem to be a shorthand for a way 
 of  avoiding coding identical algorithms that only vary in the datatypes 
 that  are passed to them. Is that it or am I missing some fundamental 
 elements?
 
 And is that why people are clamoring for implicit template instantation 
 -  to further reduce the amount of typing to do (and remembering to  
 instantiate the only the required renditions of the algorithm).
 

I feel the same. I do come from C++ background, but I just hate templates! (Now I hate C++ itself :) I think templates were introduced in C++ to solve the "no-base-class" problem. In other words, it's a hack that solves a misfeature in C++. D's templates are not really templates any more, they're more like a meta-programming facility. Or so I think, anyway. I don't really understand templates anyway, not to mention meta programming
Mar 04 2006
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Hasan Aljudy wrote:
 I feel the same.
 I do come from C++ background, but I just hate templates! (Now I hate 
 C++ itself :)
 I think templates were introduced in C++ to solve the "no-base-class" 
 problem.

It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).
 In other words, it's a hack that solves a misfeature in C++.
 
 D's templates are not really templates any more, they're more like a 
 meta-programming facility. Or so I think, anyway. I don't really 
 understand templates anyway, not to mention meta programming
 

Mar 05 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Ivan Senji wrote:
 Hasan Aljudy wrote:
 
 I feel the same.
 I do come from C++ background, but I just hate templates! (Now I hate 
 C++ itself :)
 I think templates were introduced in C++ to solve the "no-base-class" 
 problem.

It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).

No. The Object base class fits perfectly into the object oriented paradigm. Templates do not, hence a hack.
 In other words, it's a hack that solves a misfeature in C++.

 D's templates are not really templates any more, they're more like a 
 meta-programming facility. Or so I think, anyway. I don't really 
 understand templates anyway, not to mention meta programming


Mar 05 2006
next sibling parent Lars Ivar Igesund <larsivar igesund.net> writes:
Hasan Aljudy wrote:

 Ivan Senji wrote:
 Hasan Aljudy wrote:
 
 I feel the same.
 I do come from C++ background, but I just hate templates! (Now I hate
 C++ itself :)
 I think templates were introduced in C++ to solve the "no-base-class"
 problem.

It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).

No. The Object base class fits perfectly into the object oriented paradigm. Templates do not, hence a hack.
 In other words, it's a hack that solves a misfeature in C++.

 D's templates are not really templates any more, they're more like a
 meta-programming facility. Or so I think, anyway. I don't really
 understand templates anyway, not to mention meta programming



Luckily there is much more to programming than just OOP. :)
Mar 05 2006
prev sibling parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Hasan Aljudy wrote:
 Ivan Senji wrote:
 
 Hasan Aljudy wrote:

 I feel the same.
 I do come from C++ background, but I just hate templates! (Now I hate 
 C++ itself :)
 I think templates were introduced in C++ to solve the "no-base-class" 
 problem.

It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).

No. The Object base class fits perfectly into the object oriented paradigm. Templates do not, hence a hack.

Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style and C#(before templates)-style are nothing but inefficient hacks. And one more thing <insert what Lars replyed> :)
Mar 05 2006
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
news:duf72h$f40$1 digitaldaemon.com...
 Maybe so, but I see a common Object base class a hack to solve many 
 problems that are simple and efficient to solve with templates. For 
 example containers. Containers in Java(before templates)-style and 
 C#(before templates)-style are nothing but inefficient hacks.

Technically, Java and C# implement generics, not templates. C++ and D implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Walter Bright wrote:
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:duf72h$f40$1 digitaldaemon.com...
 
Maybe so, but I see a common Object base class a hack to solve many 
problems that are simple and efficient to solve with templates. For 
example containers. Containers in Java(before templates)-style and 
C#(before templates)-style are nothing but inefficient hacks.

Technically, Java and C# implement generics, not templates. C++ and D implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.

Are you saying that all generics do is silently insert casts?
Mar 05 2006
next sibling parent James Dunne <james.jdunne gmail.com> writes:
Hasan Aljudy wrote:
 Walter Bright wrote:
 
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:duf72h$f40$1 digitaldaemon.com...

 Maybe so, but I see a common Object base class a hack to solve many 
 problems that are simple and efficient to solve with templates. For 
 example containers. Containers in Java(before templates)-style and 
 C#(before templates)-style are nothing but inefficient hacks.

Technically, Java and C# implement generics, not templates. C++ and D implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.

Are you saying that all generics do is silently insert casts?

I think they'd have to be, considering Java and C# are both reliant upon serialization of code into JARs and assemblies. How would you serialize a template? -- Regards, James Dunne
Mar 05 2006
prev sibling parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Hasan Aljudy wrote:
 Walter Bright wrote:
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:duf72h$f40$1 digitaldaemon.com...

 Maybe so, but I see a common Object base class a hack to solve many 
 problems that are simple and efficient to solve with templates. For 
 example containers. Containers in Java(before templates)-style and 
 C#(before templates)-style are nothing but inefficient hacks.

Technically, Java and C# implement generics, not templates. C++ and D implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.

Are you saying that all generics do is silently insert casts?

For all interested, here's an article that explains the differences in more detail: http://www-128.ibm.com/developerworks/library/j-jtp01255.html This was posted in the NG by someone else that I don't recall, some time ago. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Mar 05 2006
prev sibling next sibling parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Walter Bright wrote:
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:duf72h$f40$1 digitaldaemon.com...
 
Maybe so, but I see a common Object base class a hack to solve many 
problems that are simple and efficient to solve with templates. For 
example containers. Containers in Java(before templates)-style and 
C#(before templates)-style are nothing but inefficient hacks.

Technically, Java and C# implement generics, not templates. C++ and D implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.

I knew that C# templates/generics are different from C++/D ones but didn't know how and why. Thanks. So C#/Java generics aren't as efficient as templates? That sucks.
Mar 05 2006
prev sibling next sibling parent reply "John C" <johnch_atms hotmail.com> writes:
"Walter Bright" <newshound digitalmars.com> wrote in message 
news:dufcpn$p1n$2 digitaldaemon.com...
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:duf72h$f40$1 digitaldaemon.com...
 Maybe so, but I see a common Object base class a hack to solve many 
 problems that are simple and efficient to solve with templates. For 
 example containers. Containers in Java(before templates)-style and 
 C#(before templates)-style are nothing but inefficient hacks.

Technically, Java and C# implement generics, not templates. C++ and D implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.

I'm sure you're wrong about C#'s implementation using casts. Java generics do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.
Mar 05 2006
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"John C" <johnch_atms hotmail.com> wrote in message 
news:dufl8l$14au$1 digitaldaemon.com...
 "Walter Bright" <newshound digitalmars.com> wrote in message 
 news:dufcpn$p1n$2 digitaldaemon.com...
 Technically, Java and C# implement generics, not templates. C++ and D 
 implement templates, not generics.

 What's the difference? One way to think about it is generics are handled 
 at run time (by casting), templates at compile time.

I'm sure you're wrong about C#'s implementation using casts. Java generics do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.

From what I read, C# generics do not instantiate a new routine for every set of argument types, it implements *one* routine which handles them all via casting.
Mar 05 2006
next sibling parent reply Don Clugston <dac nospam.com.au> writes:
Walter Bright wrote:
 "John C" <johnch_atms hotmail.com> wrote in message 
 news:dufl8l$14au$1 digitaldaemon.com...
 "Walter Bright" <newshound digitalmars.com> wrote in message 
 news:dufcpn$p1n$2 digitaldaemon.com...
 Technically, Java and C# implement generics, not templates. C++ and D 
 implement templates, not generics.

 What's the difference? One way to think about it is generics are handled 
 at run time (by casting), templates at compile time.

do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.

From what I read, C# generics do not instantiate a new routine for every set of argument types, it implements *one* routine which handles them all via casting.

It looks as though it has an optimisation for built-in types. http://www.artima.com/intv/generics2.html Clearly C# does it a lot better than Java does. Java generics seem to be 100% syntactic sugar. There's a valid point raised there about the cryptic error messages generated in C++ templates. Currently D error messages for templates are even worse, but this is mainly because it keeps tying to instantiate templates after one has failed (and you can't even press ctrl-c to stop the torrent, which can last for over a minute). This is another area where D could give a great improvement over C++.
Mar 06 2006
next sibling parent "John C" <johnch_atms hotmail.com> writes:
"Don Clugston" <dac nospam.com.au> wrote in message 
news:dugufa$2udq$1 digitaldaemon.com...
 Walter Bright wrote:
 "John C" <johnch_atms hotmail.com> wrote in message 
 news:dufl8l$14au$1 digitaldaemon.com...
 "Walter Bright" <newshound digitalmars.com> wrote in message 
 news:dufcpn$p1n$2 digitaldaemon.com...
 Technically, Java and C# implement generics, not templates. C++ and D 
 implement templates, not generics.

 What's the difference? One way to think about it is generics are 
 handled at run time (by casting), templates at compile time.

generics do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.

From what I read, C# generics do not instantiate a new routine for every set of argument types, it implements *one* routine which handles them all via casting.


I just wanted to put the record straight. For reference types, the same code is shared, but full type information is maintained, and there's no Object-to-type cast. With value types and structs, separate sections of code are created to explicitly avoid boxing/unboxing.
 It looks as though it has an optimisation for built-in types.
 http://www.artima.com/intv/generics2.html

There's a more technical overview here: http://research.microsoft.com/projects/clrgen/generics.pdf
 Clearly C# does it a lot better than Java does. Java generics seem to be 
 100% syntactic sugar.

 There's a valid point raised there about the cryptic error messages 
 generated in C++ templates. Currently D error messages for templates are 
 even worse, but this is mainly because it keeps tying to instantiate 
 templates after one has failed (and you can't even press ctrl-c to stop 
 the torrent, which can last for over a minute). This is another area where 
 D could give a great improvement over C++.

Yes, template error messages are notoriously difficult to decipher. Compilers need to address this.
Mar 06 2006
prev sibling parent Matthias Spycher <matthias coware.com> writes:
Don Clugston wrote:

 Clearly C# does it a lot better than Java does. Java generics seem to be 
 100% syntactic sugar.

Don't forget that a dynamic compiler can do anything at runtime if the byte-code carries forward enough info about the original source -- class files are extensible. I have seen anonymous local classes reduced to simple functions. With decent escape analysis, allocation on the stack is possible for small value classes. Research on this topic, IMHO, has only scratched the surface.
Mar 06 2006
prev sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 From what I read, C# generics do not instantiate a new routine for every 
 set of argument types, it implements *one* routine which handles them all 
 via casting.

Yep. And more here: Templates and Generics http://blogs.msdn.com/branbray/archive/2003/11/19/51023.aspx
Mar 06 2006
prev sibling parent reply Mark T <Mark_member pathlink.com> writes:
In article <dufcpn$p1n$2 digitaldaemon.com>, Walter Bright says...
Technically, Java and C# implement generics, not templates. C++ and D 
implement templates, not generics.

What's the difference? One way to think about it is generics are handled at 
run time (by casting), templates at compile time. 

sorry "generics" is a generic term, let's let not pigeon-hole it, Ada uses the term and it is compile time the term "template" was originally associated with patterns, "In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic programming." en.wikipedia.org/wiki/Template_(programming) "A generic term is one which picks out a class of individuals, or the prototype of the individual, rather than the individual itself. For example, in the sentence `The wolf has disappeared from northern Europe,' we are referring to the genus rather than to a particular wolf." www.informatics.susx.ac.uk/books/computers-and-thought/gloss/node1.html
Mar 08 2006
parent Georg Wrede <georg.wrede nospam.org> writes:
Mark T wrote:
 
 [T]he term "template" was originally associated with patterns,
 
 "In computer science, generics is a technique that allows one value
 to take different datatypes (so-called polymorphism) as long as
 certain contracts such as subtypes and signature are kept. The
 programming style emphasizing use of this technique is called generic
 programming." en.wikipedia.org/wiki/Template_(programming) [...]"

 www.informatics.susx.ac.uk/books/computers-and-thought/gloss/node1.html

Put like that, it becomes obvious how much of the generic stuff can be done with Interfaces. Even with the STL, all the stuff that trickles through "my custom made procedures and functions" needs certain properties. C++ did a good job of integrating the fundamental data types and structures to this. With anything that's objects, D can already be very powerful when one combines well factored interfaces with a solid set of orthogonal (not necessarily member-) functions. I love D!
Mar 09 2006
prev sibling parent reply Tom <Tom_member pathlink.com> writes:
In article <op.s5wxef016b8z09 ginger.vic.bigpond.net.au>, Derek Parnell says...
I'm sure its because I'm not from a C++ background, but I just don't get  
templates. What is the problem that Templates solve?

I'm passing away... my pressure!!! :P
 From my limited experience they just seem to be a shorthand for a way of  
avoiding coding identical algorithms that only vary in the datatypes that  
are passed to them. Is that it or am I missing some fundamental elements?

Isn't this issue enough to justify templates? I have some interesting experience with C++, I'm not an expert even though I've coded a lot in it and I've used "nearly all" (that means excluding the infamous ones such as those modern cast operators :), etc.). I have no doubts, I can say that one of the best features I've ever seen working in a language is the C++ templates. I've coded really generic functions/containers with a great success in C++ and used them with a variety of stuff. Always worked nice!
And is that why people are clamoring for implicit template instantation -  
to further reduce the amount of typing to do (and remembering to  
instantiate the only the required renditions of the algorithm).

Tom;
Mar 04 2006
parent Sebastián E. Peyrott <as7cf yahoo.com> writes:
Well, I have no real experience with templates, either, but I do think there are
many interesting (and useful) coding techniques that require them. And even
though, I, for the most part, and as Derek, see them as a way of coding similar
algorithms based on different data-types, IMO the most important characteristic
is compile-time code generation. When to use it, and how to take advantage of
that feature, be it to create abstractions of similar algorithms based on
differing data-types, or not, is a whole other issue.
I'm not entirely sure whether there are other ways of achieving the same thing,
so   templates, for the most part, seem like an important feature for a language
such as D.

--
Sebastián.
Mar 04 2006