new to Templates' concept

This is a discussion on new to Templates' concept within the C++ Programming forums, part of the General Programming Boards category; I just finished the templates chapter wich is covered in around 20 pages only in my C++ book. I understood ...

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Question new to Templates' concept

    I just finished the templates chapter wich is covered in around 20 pages only in my C++ book. I understood it's essential concepts but i need to make some things clear :

    Q1: Why would i use a template nontype parameter when i can simply add an extra argument to a function that uses that template for example?

    Q2: function\class-template specilization is the object code produced that uses the same type sent to the template argument list, so if i call a function-template with int then another time with double data ; the compiler would produce 2 specilizations.

    did i get it right?

    Q3: Do templates go under "Polymorphism"?

    Q4: Are these two template function calls different? if so , why? (arg is double) :
    Code:
    foo<double>(arg);
    Code:
    foo(arg);
    Q5: whats the difference between typename and class?



    appreciate your help
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Q1: When the second parameter should change the type. I've had a usage before where
    SomeTemplate<1> != SomeTemplate<2> and operations could not be preformed between the two.

    Q2: Yep

    Q3: My understanding is that polymorphism has more to do with inheritence. So the answer would be yes and no.

    Q4: Test it out

    Q5: spelling

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I already tested those two function calls. They both worked , but i thought they might not the be exactly the same.

    Thanks for the quick response Thantos.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    I think every thing has been covered, although, from my understanding, typename doesn't exactly equal class. int,float,double,char are all primitives, IIRC, and not so much Classes, yet they are a type, and can also be used with templates.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In a template typename and class are the same. IMO typename is better because it covers everything and there is less confusion with using class to define well a class.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Templates can be considered as "compile time polymorphism" - they let you write the same code against multiple types. I think it's also known as "parametric polymorphism" (?).

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    If you have something like this:
    Code:
    template <typename T>
    void foo(T t)
    {
    	cout<<"first"<<endl;
    
    }
    And call it like this
    Code:
    foo<double>(12.56);
    	foo(12.56);
    Then you basically have something like this:
    Code:
    	foo<double>(12.56);
    00417F1E  fld         qword ptr [__real@40291eb851eb851f (4460D0h)] 
    00417F24  sub         esp,8 
    00417F27  fstp        qword ptr [esp] 
    00417F2A  call        foo<double> (416F82h) 
    00417F2F  add         esp,8 
    	foo(12.56);
    00417F32  fld         qword ptr [__real@40291eb851eb851f (4460D0h)] 
    00417F38  sub         esp,8 
    00417F3B  fstp        qword ptr [esp] 
    00417F3E  call        foo<double> (416F82h) 
    00417F43  add         esp,8
    So these calls are same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  2. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 03:46 AM
  3. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 10:36 AM
  4. help !!concept of class templates
    By sanju in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2003, 09:12 AM

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