C++ Templates

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply

Do you use C++ Templates in your programs?

Couldn
12
38%
Sometimes they are useful
17
53%
Never needed to use them
3
9%
 
Total votes: 32

MrRage
Knows some wx things
Knows some wx things
Posts: 35
Joined: Fri Mar 11, 2005 3:43 am

C++ Templates

Post by MrRage » Mon May 23, 2005 9:23 pm

I was just wondering what you guys thought about templates and if you find them useful or not. They are powerful but I

buildere
Super wx Problem Solver
Super wx Problem Solver
Posts: 358
Joined: Thu Oct 28, 2004 3:45 pm
Location: Costa Rica

so, so

Post by buildere » Mon May 23, 2005 11:50 pm

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.

AkiraDev
Knows some wx things
Knows some wx things
Posts: 48
Joined: Tue May 24, 2005 9:13 am

Post by AkiraDev » Tue May 24, 2005 9:41 am

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
Back, just not as often

Windows Vista SP1, Ubuntu 8.04
MinGW 3.4.5, GCC 4.2.1
Code::Blocks
wxWidgets 2.8.9

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Tue May 24, 2005 10:03 am

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...
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Tom82
Experienced Solver
Experienced Solver
Posts: 58
Joined: Wed May 11, 2005 7:45 am

Post by Tom82 » Tue May 24, 2005 1:45 pm

i am using <list> and <map> very often.
both are very good Standart C Array Replacment with a lot of advantages.

Tyler
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 246
Joined: Fri Sep 03, 2004 12:37 am
Contact:

Post by Tyler » Tue May 24, 2005 3:05 pm

For me, there is no replacing my ADT templates, I use them throughout my projects. My all time favorite is the SkipList.

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: so, so

Post by Ryan Norton » Tue May 24, 2005 7:08 pm

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.

buildere
Super wx Problem Solver
Super wx Problem Solver
Posts: 358
Joined: Thu Oct 28, 2004 3:45 pm
Location: Costa Rica

Re: so, so

Post by buildere » Sat Jun 25, 2005 2:22 am

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.

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: so, so

Post by Ryan Norton » Sat Jun 25, 2005 2:37 am

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.
[Mostly retired moderator, still check in to clean up some stuff]

buildere
Super wx Problem Solver
Super wx Problem Solver
Posts: 358
Joined: Thu Oct 28, 2004 3:45 pm
Location: Costa Rica

Post by buildere » Sat Jun 25, 2005 3:18 am

Ok, thank you

Avi
Super wx Problem Solver
Super wx Problem Solver
Posts: 398
Joined: Mon Aug 30, 2004 9:27 pm
Location: Tel-Aviv, Israel

Re: so, so

Post by Avi » Sat Jun 25, 2005 9:27 am

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:

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: so, so

Post by Ryan Norton » Sat Jun 25, 2005 9:34 am

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...
[Mostly retired moderator, still check in to clean up some stuff]

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: so, so

Post by Ryan Norton » Sat Jun 25, 2005 9:37 am

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.
[Mostly retired moderator, still check in to clean up some stuff]

Post Reply