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