//Demo.cpp://definition and use of template functions://1. The template supports the return parameter type as a template parameter. //template <typename ret_t, TypeName in1_t, TypeName in2_t >//ret_t Prifunc2 (in1_t in1, in2_t in2) {...}//2. Because the return type cannot be inferred directly at the function call, the supplied template argument can be displayed. prifunc2<double, int,int>//3. When the template declaration is ret_t to the first bit, it can provide only the return value type when used, while the remaining parameters are used to infer the formal parameter in the usual way. Prifunc2<double>#include<iostream>using namespacestd;template<typename ret_t, TypeName in1_t, TypeName in2_t >ret_t Prifunc2 (in1_t in1, in2_t in2) {ret_t RET; RET= in1 +in2; cout<<"in1 ="<<in1<<Endl; cout<<"in2 ="<<in2<<Endl; RET= in1+in2; cout<<"ret ="<<ret<<Endl; returnret;}intMain () {intv = prifunc2<Double,int,int> ( One, A); intVV = prifunc2<Double> ( -, -); while(1); return 0;}//Test Results://in1 = One//in2 =//ret =//in1 =//in2 =//ret =
C + +, definition and use of template functions "primary study"