C++ templates or not

18 February 2004   0 comments  

http://www.mozilla.org/hacking/portable-cpp.html#dont_use_templates

Mind That Age!

This blog post is 14 years old! Most likely, its content is outdated. Especially if it's technical.

The Mozilla C++ portability guide says that developers should not use templates in their C++ code. Damn! I just learnt how to use templates in my Object Oriented Programming in C++ course. They are so useful that I can't understand why the compilers can't support it.

"Don't use the C++ template feature. This feature is still not implemented by all compilers, and even when it is implemented, there is great variation. Most of the interesting things that you would want to do with templates (type safe container classes, etc.) can be implemented with macros and casting, even though you do lose the type safety (pity). Often times subclassing can easily achieve the same result."

Without templates you have to write one function for every type:

void swap(int & x, int & y) 
    int tmp = x; x = y; y = tmp;
void swap(long & x, long & y) 
    long tmp = x; x = y; y = tmp;

And with templates you can generalise it like this:

template <typename T>
void swap(T &amp; x, T &amp; y) 
    T tmp = x; x = y; y = tmp;

Comments

Your email will never ever be published


Related posts

Previous:
Ugliest site of the month, yoyoguy.com 17 February 2004
Next:
Why Sun should Open up on Java 18 February 2004
Related by Keyword:
Integrate BrowserID in a Tornado web app 22 November 2011
A taste of the Django on inside Mozilla, Sheriffs Duty 22 July 2011
Future of Web Apps (quick summary and thoughts) 04 October 2007
Interesting float/int casting in Python 25 April 2006
Do you know about Firefox? 01 November 2004
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
I'm Prolog 01 May 2007
Ever wondered how much $87 Billion is? 04 November 2003