C + +, definition and use of template functions "primary study"

Source: Internet
Author: User

//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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.