C + + templates: argument deduction

Source: Internet
Author: User

In the C + + function template, the argument deduction is a very flexible mechanism, the literal meaning of the argument deduction is: To infer the type parameter of the function template by the type of the argument, give an example of a simple function template:

Template<typename t>t& func1 (t  &a,  t &b) {Cout<<typeid (t). Name () <<endl;}
Because of the argument deduction, we can use a template function just like a normal function, as follows:

int A,b;func1 (A, b);

That is, although we do not specify T for Func, the compiler can infer what t is by the type A and B, where T is inferred as int. The intent of the argument deduction is easy to understand, but there are some details that require additional attention, and now give another template:

Template<typename T>t & Func2 (t  A,  t b) {Cout<<typeid (t). Name () <<endl;}
What effect does this function template have on using T-A to replace t& a? Depending on what the argument is, if the argument is a primitive type, like the int A, b above, then the inferred T is an int when using FUNC2, but if the argument is an array, func1 and Func2 are different, simply verify that:

int A[2],b[2];func1 (A, b); Func2 (A, b);
The result of the output is: in Func1, T is inferred as int*, and in Func2, T is inferred as int [2]. What happens if we mix func1 and Func2, like the func3 below?

Template<typename T>t & Func3 (t A,  t &b) {Cout<<typeid (t). Name () <<endl;}
If you continue to use FUNC3 (A, B), the following compilation error will occur:

Error:no matching function for call to ' func3 (int [2], int [2]) '

Because in the argument deduction, the T is inferred from a int* (decay), and the t is an int [2] According to B, the two are inconsistent, so there is an error (because in the function template definition of func3, a, B should be the same type). Such errors can also occur in other situations, such as:

int const A;int Const B;FUNC3 (A, b);
At this point the compiler will output a similar error: Error:no matching function for call to ' func3 (int&, const int&) ' Because FUNC1 functions template ignores const, and the FUNC2 function template does not, volatile is the same, again such as:

void F1 () {};void F2 () {};func1 (F1,F2); Func2 (F1,F2); func3 (F1,F2);
Func1 (F1,F2) is not a problem, T is inferred as a function pointer (occurs decay), Func2 (F1,F2) is not a problem, T is inferred as an argument and the return type is empty function type, and FUNC3 (F1,F2) can not be compiled, there will be no Matching's error.


C + + templates: argument deduction

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.