Deep parsing of C + + function templates and class templates

Source: Internet
Author: User

First, function template

1. Definition

Template <class type parameter 1,class type parameter 2,...... >
Return value type template name (formal parameter list) {
function body

Template <class T1, class t2>t2 print (T1 arg1, T2 arg2) {    cout<< arg1 << "" << Arg2<<endl ;    return arg2;}

2. Do not instantiate function template by parameter

#include <iostream>using namespace Std;template <class t>t Inc (T N) {    return 1 + N;} int main () {    cout << inc<double> (4)/2;//output 2.5    return 0;}

3, function templates can be overloaded, as long as their formal parameter list or type argument table is different

Template<class T1, class t2>void print (T1 arg1, T2 arg2) {    cout<< arg1 << "" << arg2<<en DL;} Template<class t>void print (t arg1, t arg2) {    cout<< arg1 << "" << Arg2<<endl;} Template<class t,class t2>void print (t arg1, t arg2) {    cout<< arg1 << "" << Arg2<<endl;}

4. Order of function templates and functions

In cases where multiple functions and function templates have the same name, the compiler processes a function call statement as follows

    • Find the normal function (a function that is not instantiated by the template) that exactly matches the parameters.

    • Then find the template function that exactly matches the parameters.

    • Then find the normal function that can be matched after the automatic type conversion of the real parameter.

    • The above can not be found, the error.

Template <class t>t Max (t A, T b) {    cout << "Templatemax" <<endl; return 0;} Template <class t,class t2>t Max (T A, T2 b) {    cout << "TemplateMax2" <<endl; return 0;} Double Max (double A, double b) {    cout << "Mymax" << Endl; return 0;} int main () {    Max (1.2,3.4);     Output Mymax    Max (4, 5);         Output Templatemax    Max (1.2, 3);      Output TEMPLATEMAX2    return 0;}

5. No type auto-conversion when matching template functions

Template<class t>t myFunction (t arg1, t arg2) {cout<<arg1<< "" <<arg2<< "\ n"; return arg1;} ... myFunction (5, 7);      Ok:replace T with Intmyfunction (5.8, 8.4);  OK:: Replace T with Doublemyfunction (5, 8.4);    Error, no matching function for Callto ' myFunction (int, double) '

Second, class template

1. Definition

When defining a class, add one/more type parameters. When you use a class template, you specify how the type parameter should be substituted for the specific type, and the compiler generates the corresponding template class accordingly.

Template <class type parameter 1,class type parameter 2,......>//type parameter table
Class template Name {
member functions and member variables

(1) The notation of member functions in a class template:
Template <class type parameter 1,class type parameter 2,......>//type parameter table
return value type class template name < type parameter name list;:: member function names (parameter table) {
(2) Define the object's wording with a class template:
Class template name < real type parameter table > object name (constructor argument list);

Pair class template <class T1,class t2>class pair{public:    T1 key;//keyword    T2 value;//value    Pair (T1 k,t2 v): ke Y (k), value (v) {};    BOOL operator < (const pair<t1,t2> & P) const;}; Template<class T1,class T2>bool Pair<t1,t2>::operator < (const pair<t1,t2> & P) const{  // Pair's member function operator <    return key < P.key;} int main () {    pair<string,int> student ("Tom", +);   Instantiate a class pair<string,int>    cout << student.key << "<< student.value;    return 0;} Output: Tom 19

2. Defining objects with Class templates

    • The process by which a compiler generates a class from a class template is called a instantiation of a class template. Classes that are instantiated by class templates are called template classes.

    • The two template classes of the same class template are incompatible.

3. function templates as class template members

Template <class t>class a{public    :        template<class t2>        void Func (T2 t) {cout << t;}// member function template};

4. Class template and non-type parameter: Non-type parameter can appear in "< type parameter table >" of class Template

Template <class T, int size>class carray{    t array[size];p ublic:    void Print () {for        (int i = 0;i < size ; ++i)        cout << array[i] << Endl;    }; Carray<double,40> A2; carray<int,50> A3;     A2 and A3 belong to different classes

5. Class template and derivation

(1) Class templates derive from class templates

Template <class t1,class t2>              int main () {class A {                                        b<int,double> obj1;    T1 v1; T2 v2;                                C<int> obj2;};                                               return 0;template <class t1,class t2>              }class b:public a<t2,t1> {                 class B<int,double>:    T1 V3; T2 v4;                             Public a<double,int>{};                                            int v3; Double v4;template <class t>                        };class c:public b<t,t> {    T v5;};

(2) class template derived from template class

Template <class T1,class t2>class A {    T1 v1; T2 v2;}; Template <class T>class b:public a<int,double> {    T v;}; int main () {    b<char> obj1;     Automatic generation of two template classes:a<int,double> and b<char>    return 0;}

(3) class template derived from normal class

Class A {    int v1;}; Template <class t> class    b:public a {//all classes obtained from B, all with a as the base class    T V;}; int main () {    b<char> obj1;    return 0;}

(4) Normal class derives from template class

Template <class t>class A {    T v1;    int n;}; Class B:public a<int> {    double v;}; int main () {    B obj1;    return 0;}

6. Class template and friend function
(1) member functions of functions, classes, and classes as Friends of class templates

void Func1 () {}class A {};class b{public    :        void Func () {}};template <class t>class tmpl{    friend Vo ID Func1 ();    Friend class A;    friend void B::func ();}; Any class that is instantiated from TMP1 has more than three friends

(2) function template as Friend of class template

#include <iostream> #include <string>using namespace std;template <class t1,class t2>class pair{Priva Te:t1 key; Keyword T2 value;        Value Public:pair (T1 k,t2 v): Key (k), value (v) {};        BOOL operator < (const pair<t1,t2> & P) const; Template <class t3,class t4> friend Ostream & operator<< (ostream & O,const pair<t3,t4> & P);}; Template <class T1,class T2>bool pair<t1,t2>::operator < (const pair<t1,t2> & P) const{//"small" meaning Is the keyword small return key < P.key;} Template <class t1,class T2>ostream & operator<< (Ostream & o,const pair<t1,t2> & P) {O &    lt;< "(" << p.key << "," << p.value << ")"; return o;}    int main () {pair<string,int> student ("Tom", 29);    pair<int,double> obj (12,3.14);    cout << student << "" << obj; return 0;} Output: (tom,29) (12,3.14) arbitrarily from template <class T1,class t2> ostream & operator<< (Ostream & o,const pair<t1,t2> & P) The generated functions are all friends of any Pair panel class Yuan

(3) function template as Friend of class

#include <iostream>using namespace std;class a{    int v;public:    A (int n): V (n) {}    template <class t& gt;    friend void Print (const T & P);}; Template <class t>void Print (const T & P) {    cout << p.v;} int main () {    a A (4);    Print (a);    return 0;} Output: 4

(4) class template as Friend of class template

Template <class T>class b {    T v;public:    B (T N): V (n) {}    template <class t2>    friend class A;} Template <class t>class A {public    :    void Func ()  {        b<int> O (ten);        cout << o.v << Endl;    };

7. Class template and static member variable

A static member can be defined in a class template, all classes that are instantiated from that class template contain the same static members.

 #include <iostream>using namespace Std;template <class T>class A{    private:static int count;        Public:a () {count + +;}        ~a () {count--;};        A (A &) {count + +;} static void PrintCount () {cout << count << Endl;}}; template<> int a<int>::count = 0;template<> int a<double>::count = 0;int Main () {A<int> I    A    A<double> da; Ia.    PrintCount (); Da.    PrintCount (); return 0;} Output: 1 1 
Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: