Thread: function template question

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    function template question

    Just started looking at these things and have a couple quick questions.

    1) All the tutorials and the book I have use T as the argument name such as:
    template<class T> ...
    If I have several functions I want to use a template on can I use the same name for the template (T) for each of them or do they require seperate names?

    2) Is there a way I can use template on a function for all types except a particular type which will have a different function defination (overloaded function)?

    3) I have read that the template simply generates the needed object code to handle each type. Does it generate the code to handle each and every type, just the types used in the program, or just the types you have actually tried to use with that function? Or is this 100% compiler dependent?

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >>1) ...can I use the same name...for each of them or do they require seperate names?
    The scope of the type-name follows similiar rules as for variables. For function templates, the scope is the same as it is for a local variable. The name of the type-name, in this case "T", follows the same naming rules as variables do - so you're not limited to "T"

    >> 2) ...(overloaded function)?
    Sure - the compiler will always try to match the function call to the appropriate function.

    >> 3) ...or just the types you have actually tried to use with that function?
    Correct (my quoted part anyways).
    If you have a template function that isn't even called, the compiler isn't even required to compile it. This may not be what your compiler does, but VC++ 6.0 compiles the following with no errors or warnings:
    Code:
    #include <iostream>
    
    template <class Type>
    void foo(Type t)
    {
        std::cout << t;
        
        "Gobble-Dee-Goo"
    }//foo
    
    int main()
    {
        return 0;
    }//main
    gg

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    EDIT: Codeplug, you beat me!

    1) You can use the same typename (AKA class) in all your templates.

    2)
    Code:
    //template
    template <typename T>
    T func(T one, T two)
    {
       return (one + two);
    }
    
    //specialised function
    float func(float one, float two)
    {
      return ((one/2.0)+(two/3.0));
    }
    Just write the function as you would normally do for overloaded functions.

    3) According to my book, the compiler creates actual functions as it needs it, so only the ones that are used are created.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template function argument deduce
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2008, 08:56 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM