Thread: About Templates

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    About Templates

    I just had a go at using templates working off the tutorials here. i get their point and what they do, but there are some things I don't understand yet. For example whats the difference between template <class Something> and template <typename Something>?

    Also, in my code I would like to be able to check if a number is greater or equal than the number of bits in my storage type. What would be the best way to do it with this? This seems to work, but I think I read somewhere that sizeof() should only really be used for mallocing:
    Code:
    if(bit >= sizeof(IntType)*8) return false;
    Oh and why do I have to put <IntType> between the object name and the function name as in here:
    Code:
    template <class IntType> bool Flag<IntType>::FlipBit(Uint8 bit)
    There seems to be no point to it. Anyway, I'll shut up now
    Last edited by mike_g; 09-12-2007 at 10:00 PM.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    google template typename class

    sizeof could be used for that, assuming you know your target architecture (to make sure it is 8 bits a byte)

    last one I would have to look up because I am lame when it comes to templates, and still just learning like you.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    8 should be replaced with the CHAR_BIT constant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah, I'm sure someone told me that before. I knew there was something wrong with it. Cheers.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    I just had a go at using templates working off the tutorials here. i get their point and what they do, but there are some things I don't understand yet. For example whats the difference between template <class Something> and template <typename Something>?
    No difference for most usages.

    Also, in my code I would like to be able to check if a number is greater or equal than the number of bits in my storage type. What would be the best way to do it with this?
    std::numeric_limits<T> may come in handy.

    Oh and why do I have to put <IntType> between the object name and the function name as in here:
    Code:
    template <class IntType> bool Flag<IntType>::FlipBit(Uint8 bit)
    There seems to be no point to it. Anyway, I'll shut up now
    Don't honestly know the answer to that question, but it has irritated me in the past also. It probably has to do with an ambiguity in the language, but I can't imagine what it might be.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The reason for the third is that Flag is a template, thus you must specify what you're implementing. Just because there's one template argument there doesn't mean that's exactly the template argument you'd use for the class. What if you're implementing a template member? What if you're partially specializing?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM