// // main.m // test_cpp_template_01 // // Created by jeffasd on 2017/10/1. //
Copyright © 2017 year jeffasd. All rights reserved. // // C++11 New features Template parameter default type
#include <string> #include <iostream> using namespace std; template <typename
_Tp1, typename _Tp2> bool func (_Tp1 &a, _Tp2 &b) { return a > b; } // C++11 New features
Template parameter default type template <typename _Tp1, typename _Tp2 = float> bool funcDefault (_Tp1
a, _Tp2 b) { cout << typeid(a).name() << endl; cout << typeid(b).name() <<
endl; return a > b; } // Compilation error // The default type of template cannot be determined repeatedly //template <typename _Tp1,
typename _Tp2 = int> //bool funcDefault (_Tp1 a, _Tp2 b) { // cout <<
typeid(a).name() << endl; // cout << typeid(b).name() << endl; // return a > b;
//} // Lost the meaning of template function template <typename _Tp1, int = 0> bool funcDefault (_Tp1 a,
bool b) { cout << typeid(a).name() << endl; cout << typeid(b).name() << endl;
return a > b; } // Lost the meaning of template function template <typename _Tp1, int = 0> bool
funcDefault (_Tp1 a, int b) { cout << typeid(a).name() << endl; cout <<
typeid(b).name() << endl; return a > b; } // Lost the meaning of template function
// The second template parameter type can only be int Type or (class *) Pointer type ,bool Type can be compiled, but once called, it can't template <typename
_Tp1, bool = 0> bool funcDefault (_Tp1 a, bool b) { cout << typeid(a).name() <<
endl; cout << typeid(b).name() << endl; return a > b; } // Lost the meaning of template function template
<typename _Tp1, bool = 0> bool funcDefault (_Tp1 a, int b) { cout <<
typeid(a).name() << endl; cout << typeid(b).name() << endl; return a > b; }
template <typename _Tp1, typename _Tp2> bool funcDefault (_Tp1 a) { cout <<
typeid(a).name() << endl; return true; } // Template parameter type with default template type template <typename
_Tp1, int = 3> bool funcDefault (_Tp1 a) { cout << typeid(a).name() << endl;
return true; } //// Duplicate definition Compilation error //template <typename _Tp1, typename _Tp2 = float>
//bool funcDefault (_Tp1 a) { // cout << typeid(a).name() << endl; // return
true; //} template <typename _Tp1, int * = nullptr> bool funcDefault (_Tp1 a) {
cout << typeid(a).name() << endl; return true; } class Person {}; class Student
: public Person {}; #warning This usage needs attention
This usage often matches std::enable_if Use together to achieve template matching at compile time , Implementation of partial specialization process . template <typename _Tp1, Person *
= nullptr> bool funcDefault (_Tp1 a) { cout << typeid(a).name() << endl; return
true; } template <typename _Tp1, Student * = nullptr> bool funcDefault (_Tp1 a)
{ cout << typeid(a).name() << endl; return true; } // A non-type template
parameter cannot have type 'Person' // Template default type cannot be custom type //template <typename
_Tp1, Person = Person()> //bool funcDefault (_Tp1 a) { // cout <<
typeid(a).name() << endl; // return true; //} int main(int argc, const char *
argv[]) { funcDefault(3, 4.0); funcDefault<int, 3>(3, 4); funcDefault<int,
3>(3, true); // bool boolDefault = true; // funcDefault<int, boolDefault>(3,
4);// Unable to compile // funcDefault<int, boolDefault>(3, true); funcDefault<int,
float>(2.0); // call template <typename _Tp1, typename _Tp2> funcDefault<int,
int>(2); // call template <typename _Tp1, typename _Tp2> funcDefault<int, 2>(2);
// call template <typename _Tp1, int = 3> // funcDefault<int, 2.0f>(2); //
Compilation error no corresponding template function funcDefault<int, int *>(2.0); // Call template with default type template <typename
_Tp1, int = 3> funcDefault<int, Person *>(2.0); // Call template with default type template
<typename _Tp1, int = 3> funcDefault<int, Student *>(2.0); // Call template with default type
template <typename _Tp1, int = 3> }
 

Technology
©2019-2020 Toolsou All rights reserved,
data structure C Parking management of language 300 million people are working hard Java Share five face to face experience ,offer Taken ! Byte three side remote ,Java+Redis+ network + database + algorithm , Easy to kill interviewers ?C/C++ The 11th Blue Bridge Cup provincial competition B Group 2 Junior college background Java Post , Who says that there is no spring for college programmers ? new iPhone I won't support it 5G Will lead to further decline in shipment Not yet Spring? Ali P8 Summative 100 Analysis of Dao interview , Let you hammer the interviewer Upright code analysis __ Principle of two wheel balance car 【C language 】 Mine clearance game details Some suggestions for novice programmers to write practical business code in confusion