C + + Template technology: template-type conversion and parameter inference

Source: Internet
Author: User

One, type conversion and template type parameters

Templates, like function calls, use arguments passed to a template to initialize the template's formal parameters, except that the parameter is a template parameter and is usually a type parameter, and in template programming, there needs to be a very important thought transformation: in non-template programming, the variable is usually processed, and the type of the variable is the property of the variable. And in template programming, more of the type itself, we can pass the type itself as a parameter, you can infer the value of the type parameter based on the type of the argument (note that this value refers to ' type '), such as

Template <typename t> void fun (const t& param) ...

The more we deal with is the type parameter T, not the Param.

Because of the specificity of the template, the parameters are usually not converted, but a new template is generated directly, but there are exceptions, the template type conversion principle has the following several:

1) The top-level const, whether in arguments or parameters, is ignored , that is, for templates

Template <typename t>void  func (t I, t II) {    inttraits<T>:: Isint ();    Inttraits<typename add_const<t>::type>:: Isint ();    } int 0 ; Const  int 1;

Both the Func (I, CI), and func (CI, ci) inferred T are int, not const int.

When a type argument is passed into a template, the actual parameter and the top-level const of the formal parameter are stripped out of the ignore, except that the top-level const of the formal parameter in the template also requires that the control parameter cannot be changed, so it is ignored only when the parameter is passed in.

2) const conversion, you can pass a reference (or pointer) of a non-const object to a const reference (or pointer) parameter, such as the following code will not error, and three calls,T is inferred to be a string type that

 template<typename t>void  func ( const  T& const  t&) { Span style= "color: #000000;" >}  int   main () { string  x=  x   " ;  const  string  cx =  " ss  "   return  0  ;}  

3) array or function pointer conversions: If the function parameter is not a reference type, you can perform a normal pointer conversion on the arrays or function type arguments. However, if it is a reference type, it is not converted to a pointer


C + + Template technology: template-type conversion and parameter inference

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.