templates

This is a discussion on templates within the C++ Programming forums, part of the General Programming Boards category; hmm ,i just figured out about templates?but i have one question? should they be used in programming alot or are ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    templates

    hmm ,i just figured out about templates?but i have one question?

    should they be used in programming alot or are there certain cases..i dont want to learn something not usefull

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    They are very useful, especially for general-purpose functions like sorting or searching functions.

    [edit] They weren't included in Java originally because it was thought that they were too compilcated, by the latest version of Java has them. They put them in because they were tired of overloading functions for every data type. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    thank you ,im sure ill get the hang of them

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,577
    They should be used when needed. The type of project you are working on will dictate if that means a lot ot not.

    At the very simplest level of usage, templates enable the coding of type independent functions. Whether you need this or not is obviously a matter of what you are doing and your personal preferences. Certainly a template is a much cleaner choice than overloading 4 functions to do the same exact thing.

    At a more complex level, templates serve as a way to code classes and functions that are polymorphic across non related types. This is a powerful coding practice known as Generic Programming. Here the coder doesn't have many choices. He may be forced into this model by his project at the expense of, if not doing it, having a much more complex and hard to manage code. As an example, the STL wouldn't have been as popular and as powerful as it is today if it didn't rely on generic programming.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You will certainly need to use code that uses templates, but you won't actually write templated code all that often, especially as a relative beginner to the language. As long as you understand how to use them, I would just get a quick overview of writing them and save the deep study until you feel you are advanced in other areas of C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 03:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 10:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21