- Examples
Let's check out the examples of template arguments.
We'll cover the following...
Example 1: Deduction of Template Arguments
Press + to interact
// templateArgumentDeduction.cpp#include <iostream>template <typename T>bool isSmaller(T fir, T sec){return fir < sec;}template <typename T, typename U>bool isSmaller2(T fir, U sec){return fir < sec;}template <typename R, typename T, typename U>R add(T fir, U sec){return fir + sec;}int main(){std::cout << std::boolalpha << std::endl;std::cout << "isSmaller(1,2): " << isSmaller(1,2) << std::endl;// std::cout << "isSmaller(1,5LL): " << isSmaller(1,5LL) << std::endl; // ERRORstd::cout << "isSmaller<int>(1,5LL): " << isSmaller<int>(1,5LL) << std::endl;std::cout << "isSmaller<double>(1,5LL): " << isSmaller<double>(1,5LL) << std::endl;std::cout << std::endl;std::cout << "isSmaller2(1,5LL): " << isSmaller2(1,5LL) << std::endl;std::cout << std::endl;std::cout << "add<long long int>(1000000,1000000): " << add<long long int>(1000000, 1000000) << std::endl;std::cout << "add<double,double>(1000000,1000000): " << add<double,double>(1000000, 1000000) << std::endl;std::cout << "add<double,double,float>(1000000,1000000): " << add<double,double,float>(1000000, 1000000) << std::endl;std::cout << std::endl;}
Explanation
In the above example, we have defined 3 function templates
isSmaller
takes two arguments which have the same type and returnstrue
if the first element is less than the second element (line 6). Invoking the function with arguments of different types would give a compile-time error (line 25).isSmaller2
takes two arguments which can have a different type. The function returnstrue
if the first element is less than the second element (line 11).add
takes two arguments which can have different types (line 16). The return type must be specified because it cannot be deduced from the function arguments.
Example 2: Template Default Arguments
Press + to interact
// templateDefaultArgument.cpp#include <functional>#include <iostream>#include <string>class Account{public:explicit Account(double b): balance(b){}double getBalance() const {return balance;}private:double balance;};template <typename T, typename Pred= std::less<T> >bool isSmaller(T fir, T sec, Pred pred= Pred() ){return pred(fir,sec);}int main(){std::cout << std::boolalpha << std::endl;std::cout << "isSmaller(3,4): " << isSmaller(3,4) << std::endl;std::cout << "isSmaller(2.14,3.14): " << isSmaller(2.14,3.14) << std::endl;std::cout << "isSmaller(std::string(abc),std::string(def)): " << isSmaller(std::string("abc"),std::string("def")) << std::endl;bool resAcc= isSmaller(Account(100.0),Account(200.0),[](const Account& fir, const Account& sec){ return fir.getBalance() < sec.getBalance();});std::cout << "isSmaller(Account(100.0),Account(200.0)): " << resAcc << std::endl;bool acc= isSmaller(std::string("3.14"),std::string("2.14"),[](const std::string& fir, const std::string& sec){ return std::stod(fir) < std::stod(sec);});std::cout << "isSmaller(std::string(3.14),std::string(2.14)): " << acc << std::endl;std::cout << std::endl;}
...