Templates are fun

Posted on:May 25 2007

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.



Templates can be good though, particularly if they're well used and made clearer through copious typdef usage. Boost and Loki are awesome.
2007-05-25 19:54:00

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.
2007-05-25 20:40:00

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
2007-05-25 21:53:00

Well i dont really use em.

ASpanishGuy, is D really that nice?

2007-05-25 23:15:00

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.
2007-05-26 00:32:00

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
2007-05-26 11:24:00

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).
2007-05-26 12:16:00

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

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

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.
2007-05-26 17:45:00

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.
2007-05-27 13:05:00

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

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

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!
2007-05-27 21:28:00

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).
2007-05-27 22:30:00

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 :)
2007-05-27 22:38:00

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. ;-)
2007-05-28 09:38:00

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 :)
2007-05-28 10:48:00

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.
Quite interestingly he mentions Haskell a lot in spite of its horrible syntax.
2007-05-28 22:26:00

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?
2007-05-31 22:36:00

Add comment:

Posted by:

Enter the missing letter in: "Internati?nal"




Possible Codes

Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]