First Principles of C++: Generic programming and function templates

First Principles of C++: Generic programming and function templates

C++ allows to write a function that can be used independent of datatypes using template. So that you don't have to write separate functions to handle different types like we do in c programming.

Template Definition is the blueprint of the function that can accept any primitive datatype.

use "template <class T>" or "template <typename T>" just before the function. T is placeholder for the datatype. here is an example

#include <iostream>
/**** Template Definition ****/
template <class T>
T max(T a, T b) {
    return (a>b) ? a : b;
}        

Based on the usage in the program Compiler generates the code for the respective datatypes called "Template Instantiation". In below example, compiler will do the job of generating code for you for the each data-type used.

It is good practice to mention the type of the data within < > used in the function so that compiler and program maintainer are clear.

int main() {
    int a = 5, b = 6;

    // Instantiates max<int>(int, int)
    std::cout << "integer:" <<max(a, b) <<std::endl;
    // max<int> (a, b) and max(a, b)  are same
 
    // Instantiates max<char>(int, int)
    std::cout << "char:" <<max<int>('a', 'b') <<std::endl; 

    // Instantiates max<double>(int, int)
    std::cout << "Double:" <<max(29.4, 30.5) <<std::endl;

    return 0;
}         

Interesting behavior with user defined datatype

Here in example we have a class Student, members as name and total. stud1 and stud2 are object of Student class.

struct Student {
    int total;
    std::string name;
    Student(std::string name, int t): name(name), total(t)  {  }
};

int main() {
    Student stud1("Krish",500);
    Student stud2("Arjun", 450);
    
    Student res = max(stud1, stud2);
    std::cout << res.total << " by " << res.name <<std::endl;
    return 0;
}        

When we call template function with the objects of Student class like " max(stud1, stud2) " function tries to compare two objects " stud1 > stud2 ", unlike primitive type, C++ compiler does not known what to compare here. So, it is programmer responsibility to define how class object should behave with the help of operator overloading.

stud1 > stud2 has three part. [ Return type, left object, operator, right object ]

here this condition > greater than is going to compare the total marks of stud1 and stud2.

Return True if stud1.total is greater than stud2.total and return False otherwise.

struct Student {
    int total;
    std::string name;
    Student(std::string name, int t): name(name), total(t)  {  }
    
    /**** Operator overloading *****/
    bool operator>(Student s){
        return (this->total > s.total) ? true : false;
    }
};        

So in this case, max(stud1, stud2) is going to return Student type. Using that we can access the maximum total also the other data members like name.

here is the full program, get the quick hand-on with the online c++ compiler.

#include <iostream>

template <class T>
T max(T a, T b) {
    return (a>b)?a:b;
}

struct Student {
    int total;
    std::string name;
    Student(std::string name, int t): name(name), total(t) {  }

    /**** Operator overloading *****/
    bool operator>(Student s){
        return (this->total > s.total) ? true:false;
    }
};

int main() {
    int a = 5, b = 6;
    std::cout << "integer:" <<max(a, b) <<std::endl;
    std::cout << "char:" <<max<int>('a', 'b') <<std::endl;
    std::cout << "Double:" <<max(29.4, 30.5) <<std::endl;

    Student stud1("Krish",500);
    Student stud2("Arjun", 450);
    
    Student res = max(stud1, stud2);
    std::cout << res.total << " by " << res.name <<std::endl;
    return 0;
}

/***** Output 
integer:6
char:98
Double:30.5
Double:500 by Krish
******/        



要查看或添加评论,请登录

M Vijaynath的更多文章

社区洞察

其他会员也浏览了