Thread: templates?????

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post templates?????

    can anybody tell me what is the usage of templates in a cpp file
    when do we use it and why??if its possible can u give me a simple example about .t

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Templates are used when you dont know the exact type of variable you want to use. Say you dont know if its going to be int or char. You use a template function which is good for all variable types.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    templates allow you to create functions and classes independant of the types that are applied to them....

    You can take a common idea (maybe a container or smart pointer) and provide the code nessasary without having to concentrate too much if this code will be used with say an int or a char or a user defined class. The compiler uses the template to create the actual code needed (say a version for int, a version for char...etc)

    So you can do this with 1 temaplate;

    Code:
    #include <iostream>
    #include <string>
    
    
    template <class T>//template function
    void Print(const T& t){
    	std::cout << t << std::endl;
    }
    
    int main(){
    	
    	std::string str("Hello World");
    	
    	int x = 20;
    	
    	double d = 569.789;
    	
    	//compiler produces code to make a Print for string,int and double
    	Print(str);
    	
    	Print(x);
    	
    	Print(d);
    
    }
    And the compiler will turn this to;

    Code:
    #include <iostream>
    #include <string>
    
    void Print(std::string& t){
    	std::cout << t << std::endl;
    }
    
    void Print(double& t){
    	std::cout << t << std::endl;
    }
    
    void Print(int& t){
    	std::cout << t << std::endl;
    }
    
    int main(){
    	
    	std::string str("Hello World");
    	
    	int x = 20;
    	
    	double d = 569.789;
    	
    	Print(str);
    	
    	Print(x);
    	
    	Print(d);
    
    }
    The benefit to you is that you need only define your code once...not for every possible value that can be applied to it

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    when do we use it and why??if its possible can u give me a simple example about .t
    Datastructures, e.i stack and queue.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >when do we use it and why??
    You use templates when the type of the data will change yet not effect the behavior. An example is a vector, you can have a vector of almost any type yet the behavior of the vector itself remains the same, this is a prime example of when a template is an excellent solution.

    -Prelude
    My best code is written with the delete key.

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