Thread: When and when not to use templates

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    When and when not to use templates

    A fairly large project im currently working on originally extensively used templates. Then I decided to change a portion of it to use regular arguments instead (for inheritence purposes). Then I realized the benifit of using arguments were useless in this case and then I changed it back. Now Im starting to consider changing it back to use regular arguments again!

    My question is what is a good method to determine whether you should use templates or regular arguments, ignoring the cases when using the choice to use templates is obvious
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No hard and fast rules, but here are a few guidelines I use:

    When to use templates:
    - When efficiency is a concern. (An example, I made a PRNG class a while back - simple linear congruential generator - and I wanted to optimize some of the special cases for better efficiency).
    - For compile time checks and typedefing (traits classes for example).
    - When the internal algorithms of an program can be changed, but rarely are (STL allocators for example).

    When not to use them:
    - When it will cause a proliferation of new types and it can be avoided through inheritance or normal parameters (without causing the same proliferation of classes).

    Anyways... thats generally what I do.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Perhaps a quote from "Effective C++" might help:
    Does the type T affect the behavior of the class? If T does not affect the behavior, you can use a template. If T does affect the behavior, you'll need virtual functions, and therefore use inheritance

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You can happily mix inheritance and templates almost to your hearts content. Check out James Coplein's Curiously Recurring Template Pattern to see a template relationship that uses templates in one direction and normal inheritance in the other.
    Check out the ATL and WTL source to see that in action.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I write template functions when a function does something generic that it could do to several different types. I also write them so that I can just write one function instead of one for every type of argument I want it to accept.

    Maybe I'm being naive, but wouldn't you just make a function a template whenever you want it to accept more than one data type? That seems like the obvious answer to me. Feel free to prove me wrong so that I may learn.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    The answer depends on you. If you are ADDING templates to a project, it is a grueling task of finding where and what you have to change. If you are starting and you are wondering if you should use templates, its up to you.
    Ask:
    "What is a template used for?"
    "Is that what I am doing?"
    "What will it accomplish?"
    "Will it slow me down?"
    "Will it make errors appear?"
    Stuff like that and weigh the options. If your asking if you should, you might as well right?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Also remember that template parameters need not be types.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Thanks for all your replies, they were very helpful.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed