Page 1 of 1

C++ Templates

Posted: Mon May 23, 2005 9:23 pm
by MrRage
I was just wondering what you guys thought about templates and if you find them useful or not. They are powerful but I

so, so

Posted: Mon May 23, 2005 11:50 pm
by buildere
To me, Templates are a good idea and very useful in some situations; but sadly, when I've used them my programs compiles like 10x slower than without them. One of the reasons I don't understand why wxWidgets is now oriented to use STL internally on some classes, according to what I've heard. Maybe someone could explain this in more detail, and tell if I'm wrong about how slow they are at compiling time.

Posted: Tue May 24, 2005 9:41 am
by AkiraDev
Hi!

I voted in the first option.

IMHO, C++ templates are what make C++ much superior to most "modern" programming languages such as Java or it's clone C#.
The problem is, their capabilities are underestimated and in result, they are misused.
An example are school projects in which templates are merely used in the boring old "generic container example", which gives a completely wrong idea about their best applications.
You can use them to enforce type-safety (enter traits and policies here), force and tune function inlining, and meta-programming, just to name a few techniques. For example, combining policies with meta-programming in a library, it is possible to have the compiler generate adaptive algorithms which require minimal code from the library user. AFAIK, C++ is the only programming language capable of meta-programming.

Projects like WTL (although from MS) make extensive use of templates to make GUI programs lightweight, streamlined and fast. There are plenty of oher examples on this. I still hope that one day wxWidgets will adopt templates as a design concept (not just inheritance from the STL).

Best regards

Posted: Tue May 24, 2005 10:03 am
by upCASE
Hi Akira!
Good to see you back ! :D

You're right: Templates are important and make C++ unique.
I voted for the second option nonetheless, as there are project in which I do use them and others where I don't. It depends...

As for the schools, I think that they doesn't teach it, because it's too time consuming. And in fact I think that this is quite an advanced feature some student couldn't understand... During my education we really had to pressure our teacher to give some lessons about even the most simple stuff in the STL. I suppose she didn't even know about that herself...

As a matter of fact, I do believe that people turn to Java or C# because the feel like C++ misses support for features like vectors or dynamic arrays. It's just easier to have this as a "class type" build in, than to learn about the mighty template ideology. Back in "college" (I can't translate this any better), I heard many people say "you can't do this and that in C++", which is just not true. They even pityed us for writing a pretty big project in C++ using Qt, while in fact it would have been much more work in Java...

Posted: Tue May 24, 2005 1:45 pm
by Tom82
i am using <list> and <map> very often.
both are very good Standart C Array Replacment with a lot of advantages.

Posted: Tue May 24, 2005 3:05 pm
by Tyler
For me, there is no replacing my ADT templates, I use them throughout my projects. My all time favorite is the SkipList.

Re: so, so

Posted: Tue May 24, 2005 7:08 pm
by Ryan Norton
buildere wrote:To me, Templates are a good idea and very useful in some situations; but sadly, when I've used them my programs compiles like 10x slower than without them. One of the reasons I don't understand why wxWidgets is now oriented to use STL internally on some classes, according to what I've heard. Maybe someone could explain this in more detail, and tell if I'm wrong about how slow they are at compiling time.
In 2.6 the STL is used by default in a few classes already.

For a template example:

Code: Select all

std::map<int, int> map1;
std::map<int, long> map2;
For what seems like something simple, it actually has to recompile std::map and creates entire copies of std::map for each of those declarations and stores it and its functions addresses etc. in the executable/dll.

There are some ways to sort of get around this. For one, you can do sort of what wxArray classes do and have only small functions that inline themselves in the code so there's no copies of the methods stored anywhere - this can be rather compiler-dependant though.

In the end its a trade-off between type-safety and variable speed increases (since instead of using operator = you'd have to dynamically allocate if you use void*s) versus compile time and executable size.

It should be noted the Embedded C++ Standard does not support templates http://www.caravan.net/ec2plus/.

I personally sometimes use templates for array-type classes and events. I don't use the STL, if you do though you might want to look into http://www.bdsoft.com/tools/stlfilt.html (STLFILT) that might help you understand those annoying stl warnings and errors MSVC and other compilers shell out.

(Actually some projects I don't use the standard C runtime which means on some compilers I can't use virtual functions :)).

Basically, my point is that Akira is right that templates are far too often used for large array-type template classes like std::map and generally template classes should consist mostly of small, inlineable methods so that there's no "duplication" of function definitions in the project targets.

Re: so, so

Posted: Sat Jun 25, 2005 2:22 am
by buildere
Ryan Norton wrote:
buildere wrote:To me, Templates are a good idea and very useful in some situations; but sadly, when I've used them my programs compiles like 10x slower than without them. One of the reasons I don't understand why wxWidgets is now oriented to use STL internally on some classes, according to what I've heard. Maybe someone could explain this in more detail, and tell if I'm wrong about how slow they are at compiling time.
In 2.6 the STL is used by default in a few classes already.

For a template example:

Code: Select all

std::map<int, int> map1;
std::map<int, long> map2;
For what seems like something simple, it actually has to recompile std::map and creates entire copies of std::map for each of those declarations and stores it and its functions addresses etc. in the executable/dll.

There are some ways to sort of get around this. For one, you can do sort of what wxArray classes do and have only small functions that inline themselves in the code so there's no copies of the methods stored anywhere - this can be rather compiler-dependant though.

In the end its a trade-off between type-safety and variable speed increases (since instead of using operator = you'd have to dynamically allocate if you use void*s) versus compile time and executable size.

It should be noted the Embedded C++ Standard does not support templates http://www.caravan.net/ec2plus/.

I personally sometimes use templates for array-type classes and events. I don't use the STL, if you do though you might want to look into http://www.bdsoft.com/tools/stlfilt.html (STLFILT) that might help you understand those annoying stl warnings and errors MSVC and other compilers shell out.

(Actually some projects I don't use the standard C runtime which means on some compilers I can't use virtual functions :)).

Basically, my point is that Akira is right that templates are far too often used for large array-type template classes like std::map and generally template classes should consist mostly of small, inlineable methods so that there's no "duplication" of function definitions in the project targets.
Thanks for your answer Ryan (I've just found it today (gosh, this forum grows so damn fast :shock: ))

I just saw this entry on the summary of Julian's new book at

http://wxwidgets.org/book/index.htm

Chapter 13: Data structure classes
Why not STL? ...


I haven't readed the book yet, but I wonder... is it the official policy to use or to avoid STL in wxWidgets? I think I'm missing something here, maybe you as a wx-dev could tell us more about this.

Re: so, so

Posted: Sat Jun 25, 2005 2:37 am
by Ryan Norton
buildere wrote: I haven't readed the book yet, but I wonder... is it the official policy to use or to avoid STL in wxWidgets? I think I'm missing something here, maybe you as a wx-dev could tell us more about this.
By default in 2.6.1 the STL is used (the wx classes are just wrappers around the STL ones).

There's may be a heavy debate eventually on whether to keep those classes in wxBase though - some devs want it and some dont I guess.

Posted: Sat Jun 25, 2005 3:18 am
by buildere
Ok, thank you

Re: so, so

Posted: Sat Jun 25, 2005 9:27 am
by Avi
Ryan Norton wrote:By default in 2.6.1 the STL is used (the wx classes are just wrappers around the STL ones).
I think not... at least the wxMSW build (when using Microsoft Visual Studio 2003)... I had to modify "#define wxUSE_STL 0" to "#define wxUSE_STL 1" in /include/wx/msw/setup.h... :roll:

Re: so, so

Posted: Sat Jun 25, 2005 9:34 am
by Ryan Norton
Avi wrote:
Ryan Norton wrote:By default in 2.6.1 the STL is used (the wx classes are just wrappers around the STL ones).
I think not... at least the wxMSW build (when using Microsoft Visual Studio 2003)... I had to modify "#define wxUSE_STL 0" to "#define wxUSE_STL 1" in /include/wx/msw/setup.h... :roll:
You're correct - Hmmm... it was at one point it WAS the default... maybe for 2.6 they reverted it or something...

Re: so, so

Posted: Sat Jun 25, 2005 9:37 am
by Ryan Norton
Ryan Norton wrote:
Avi wrote:
Ryan Norton wrote:By default in 2.6.1 the STL is used (the wx classes are just wrappers around the STL ones).
I think not... at least the wxMSW build (when using Microsoft Visual Studio 2003)... I had to modify "#define wxUSE_STL 0" to "#define wxUSE_STL 1" in /include/wx/msw/setup.h... :roll:
You're correct - Hmmm... it was at one point it WAS the default... maybe for 2.6 they reverted it or something...
Ahhh... I see... but default the STL streams and string is used, but not the other containers (wxUSE_STD_STRING) at least with configure/make.