- Examples
In this lesson, we will look at some examples of template parameters.
We'll cover the following...
We'll cover the following...
Example 1
C++
// templateTypeParameter.cpp#include <iostream>#include <typeinfo>class Account{public:explicit Account(double amt): balance(amt){}private:double balance;};union WithString{std::string s;int i;WithString():s("hello"){}~WithString(){}};template <typename T>class ClassTemplate{public:ClassTemplate(){std::cout << "typeid(T).name(): " << typeid(T).name() << std::endl;}};int main(){std::cout << std::endl;ClassTemplate<int> clTempInt;ClassTemplate<double> clTempDouble;ClassTemplate<std::string> clTempString;ClassTemplate<Account> clTempAccount;ClassTemplate<WithString> clTempWithString;std::cout << std::endl;}
Explanation
-
In the above code, we identify the different data types that we have passed in the parameter list.
-
We identify the type of variable passed to the function by using the keyword
typeidin line 25. If we pass astringor aclasstype object in the parameter list, it will display the type of parameter ...