How to obtain a C + + template argument inference result

Source: Internet
Author: User

In the fifth edition of C + + Primer, about the template argument inference, the following content is described

1. Type conversion and template type parameters

2. Explicit arguments for function templates

3. Tail return type

4. Type conversion

5. Function pointers and argument inference

6. Template parameter Inference and reference

The book leaves you with some exercises that require you to give the inferred type of the template parameters, see 16.2.5 exercises. The question is how to verify that our answers are correct, and I'm here to give you a piece of code!

<span style= "FONT-SIZE:14PX;" >template <typename t> void G (t&& val) {if (Is_lvalue_reference<t>::value) {if (is_const<ty Pename remove_reference<t>::type>::value) {cout<< "Const left reference of" <<typeid (T). Name ()    <<endl;}  else{cout<< "left reference of" <<typeid (T). Name () <<endl;}} else if (is_rvalue_reference<t>::value) {if (Is_const<typename remove_reference<t>::type>::value)    {cout<< "Const Right reference of" <<typeid (T). Name () <<endl;  else{cout<< "Right Reference of" <<typeid (T). Name () <<endl;}} else Cout<<typeid (T). Name () <<endl;} ------------------------------------------------------------</span><pre name= "code" class= "CPP" > <span style= "FONT-SIZE:14PX;" >template <typename t> void g (const t& val) {if (Is_lvalue_reference<t>::value) {if (is_const<t Ypename remove_reference<t>::type>:: Value) {cout<< "Const left reference of" <<typeid (T). Name () <<endl;  else{cout<< "left reference of" <<typeid (T). Name () <<endl;}} else if (is_rvalue_reference<t>::value) {if (Is_const<typename remove_reference<t>::type>::value)    {cout<< "Const Right reference of" <<typeid (T). Name () <<endl;  else{cout<< "Right Reference of" <<typeid (T). Name () <<endl;}} else Cout<<typeid (T). Name () <<endl;} ---------------------------------------------------------------</span><pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" ><span style= "FONT-SIZE:14PX;" >template <typename t> void G (T val) {if (Is_lvalue_reference<t>::value) {if (Is_const<typename rem Ove_reference<t>::type>::value) {cout<< "Const left reference of" <<typeid (T). Name () <<    Endl;}  else{cout<< "left reference of" <<typeid (T). Name () <<endl;}}else if (is_rvalue_reference<t>::value) {if (Is_const<typename remove_reference<t>::type>::value)    {cout<< "Const Right reference of" <<typeid (T). Name () <<endl;  else{cout<< "Right Reference of" <<typeid (T). Name () <<endl;}} else Cout<<typeid (T). Name () <<endl;} </span>



After that, we can test the correctness of our guesses with the following code.

<span style= "FONT-SIZE:14PX;" >int Main () {  double i=0;const double ci=i;  g (i);  g (CI);  g (I*CI);  g (I=CI);  return 0;} </span>
I will not read more about the contents of the book, you can browse the relevant books by themselves.


How to obtain a C + + template argument inference result

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.