Templates are fun

Just came across some code I wrote some time ago in irrKlang and was shocked:

template< typename T >
class CMyImpl : public T
{
  template< class A, class B, class C, class D, class E,
            class F, class G, class H, class I >
  CMyImpl(A a, B b, C c, D d, E e, F f, G g, H h, I i)
  : T(a,b,c,d,e,f,g,h,i)
  {
  }
}


Some parts have been removed, but the code really looks like this and is being used. When I started learning C++ about a decade ago, and I came across code looking like this, I usually was shocked and scrolled away. Looks like now I've become one of those template abusing programmers as well. :)
But joking aside: I still try to avoid templates and only use them for simple types operating on different datatypes like the Irrlicht's and irrKlang's vector 3D because it confuses other programmers very easily.

19 comments, already:

Eek.

Templates can be good though, particularly if they’re well used and made clearer through copious typdef usage. Boost and Loki are awesome.
Steve () (link) - 25 05 07 - 19:54

C++ Templates sucks, D ones not. Change your mind :). And Boost is a pain in the ass when you have got a 100 lines template error.
ASpanishGuy - 25 05 07 - 20:40

Templates can be a wonderful think to work,if used right and not to much.
I have no idea what the code above do and why he calls an constructor with 9 parameters, all template, i simply can not see any use in it.
If you want to see some really cool template usage look here, at the first look you doesnt understand how it work, but after programming it i was shocked how code this code was. http://tutok.sk/fastgl/callback.html
mirlix () - 25 05 07 - 21:53

Well i dont really use em.

ASpanishGuy, is D really that nice?

greets,
halan
halan () - 25 05 07 - 23:15

i love templates
boost.lambda is a great example of the power of templates.
lambdas is something that needs to go into the language, not into a library. but the fact that it is even possible to write a library for it using templates shows how powerful templates are.

nico, i like to see a blog entry with a code block ;)
i wish there were more.
pyro - 26 05 07 - 00:32

Hey guys what does Boost do for templates? I have only heard of boost::asio boost threading, and some string searching thing in boost…ok there are millions of things haha
BlindSide - 26 05 07 - 11:24

What would containers be without templates? All STL algorithms are template based, without it wouldn’t simply work as expected. And with template specialization and template meta-programming you can do even more wonderful stuff :-)
BTW: The new C++ standard C++0x is almost finished and will include boost.lambda and template meta programming (besides several other interesting stuff).
hybrid - 26 05 07 - 12:16

hybird, i though that C++0x will be released in 2009.
do you have any information on that?
pyro - 26 05 07 - 12:53

C++0x is meant to be release in 200x (means 2000-2009)
There is no official release date yet.
Sylence - 26 05 07 - 14:04

The last ballot (standards voting mechanim in the ISO) is scheduled for later this year. All features must be fixed until then, otherwise the final standard cannot be published before 2010. But the current feature list can be found on several web sites. I just read in a german computer magazine about the latest situation.
hybrid - 26 05 07 - 17:45

Some programmers have joked that the x in C+0x is a hexadecimal number ;-) So we have still 8 years for C+0x to become an official standard.
wolfgke - 27 05 07 - 13:05

To halan

i don’t want to be a D troll here, but D templates rocks, lastest version of D(not v1.0) support CTFE, so you can see C++ metaprograming(current and C++0x) using templates like a thing of the past. That together with “static if”s and mixmins, makes D the most powerfull template language out there

Check this Thread, and enjoy what you can do do
http://www.digitalmars.com/d/archives/di..

Of course, C++ and D and “insert you imperative language” sucks. Functional languages like Ocaml are better, but not anybody can program/understand them.
ASpanishGuy - 27 05 07 - 15:59

There are situations where functional languages might be of some use, but my recent experiences with Haskell support my opinion that there are only few of those. Monadics suck!
hybrid - 27 05 07 - 21:28

Well, all you can do with an imperative language is posible with a functional language, and Visual Basic is an imperative language, it sucks hardly, and that doesn’t mean that all imperative languages sucks.

BTW, OCaml can be programmed in an imperative, functional or procedural way, it supports all :P. Only his Type inference sucks when you want to do some functions ala C++ templates(and yes, type inference rocks in some other things).

Ocaml is not famous/widely used because there’s no a big company like M$ or Sun with a lot of moeny pushing it. The same thing went to Lisp(well, Lisp ((had (som other) (problem))).
And yes, they are not so hyped like Ruby…

D templates are nearer to functional languages than C++.
And write those words in your mind, D is more suitable for game programming than C++. C++ must dead. C++ is going to be the Cobol of game programming(Remenber, in the server side, Java is Cobol).
ASpanishGuy - 27 05 07 - 22:30

It doesnt look that complicated. Just has a whole heap of classes in the template def, and then defaulting the predefined (small letter) classes to the big ones. What I am very curious about however is what the heck this thing is used for. Can you post up the body of thefunction? Pretty please :)
BlindSide - 27 05 07 - 22:38

lol i dont know much of c++ so i am as lost as a ‘sheep in the big city’.. i only know c#, java and vb.net. may be i should spend some time and get to learn it. ;-)
leo () (link) - 28 05 07 - 09:38

I don’t think templates are bad, I really like them, especially for reducing the amount of code for implementations. But I try to avoid them for public interfaces when others need to use my code. Nice dicussion here :)
niko - 28 05 07 - 10:48

Templates are great. Actually, the widely-used STL is named that way because of them.
For costumer-end code I try to use them wherever its syntax doesn’t obscure the semantics (mostly for container-like classes).

Anyway, it’s true that C++ templates need some fixes. I’d love template namespaces or template typedefs. Recently I read an
article about C++0x “concepts”, the new cool feature for templates (www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1886.pdf).

By the way, I don’t remember if I read it here or somewhere else, so here is the link to an article written by a developer of the Unreal Engine where he talks about what would really make a language more suitable for game programming.
(www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf)
Quite interestingly he mentions Haskell a lot in spite of its horrible syntax.
salcerdon () - 28 05 07 - 22:26

WOW, I didn’t realise that so many people seem to hate Haskell. I personally really enjoyed learning and programming with Haskell. The syntax, while different than a C-based language, doesn’t seem to throw up any red flags to me. I always thought most programmer enjoyed programming in Haskell once they got their minds wrapped around using an entirely functional language. Hmmmm, thought? Anyone else like Haskell?
TSM - 31 05 07 - 22:36


Name:  
Remember personal info?
yes
no
Email (optional):
URL (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile

  ( Register your username / Log in )

Notify: Yes, send me email when someone replies.  

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.