About Templates

This is a discussion on About Templates within the C++ Programming forums, part of the General Programming Boards category; I just had a go at using templates working off the tutorials here. i get their point and what they ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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,893
    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, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  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

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