C ++ template syntax

Source: Internet
Author: User
Tags bitset

C ++ template syntax

Function template features

template <class T>int compare(T v1,T v2){if(v1<v2)return -1;else if(v1>v2)return 1;elsereturn 0;}template <>int compare<char *>(char * s1,char * s2){    return strcmp(s1,s2);}

Class template Specialization

template <class T>class test{public:void operator()(){cout<<"test<T>"<<endl;}};template <>  class test<char>{public:void operator()(){cout<<"test<char>"<<endl;}};
Biased templates

template <class T,class O>  struct testClass{testClass(){cout<<"I,O"<<endl;}};template <class T>struct testClass<T *,T *>{testClass(){cout<<"T*,T*"<<endl;}};
Member Template

template <class T>class v{public:template <class T1>void insert(int position ,T1 t){cout<<"insertion"<<endl;}};

Determine the current template parameters based on the previous Template

template <class T,class S=vector<T> >   class test{public:test(){cout<<"test"<<endl;}private:S t; }

Binding template to youyuan

template <class T>   class Queue{friend bool operator == <T> (const Queue <T> &t1,const Queue <T> &t2){cout<<"T,T"<<endl;return true;}};

Inline Function Template

inline template <typename T> T min(const T& a,const T & b){return a<b?a:b;}
Note that the positions of inline and template cannot be exchanged.

Type specified in the template

Template <class T> class test {public: typename T: size_type t; // template internal definition type };

Non-type template parameters

template <class T,size_t N>void arr(T (&para)[N]){int i=0;for(i=0;i<N;i++)cout<<para[i]<<endl;};



In c ++, what is the syntax of the template <class T> class?

The template features static polymorphism and polymorphism during the compilation period, for example:

Template <class T>
Void fun (){}

Fun (1 );
Fun (1, 2.3 );

The compiler will only generate void fun <int> () and void fun <double> () for you. This check is performed during compilation.

For example, you can use this feature to create a compile time check, also known as static check. For example, on morden C ++ design:

Template <bool>
Struct static_assert;

Template <>
Struct static_assert <true> {};

You can implement assert during compilation;

Static_assert <1> 2> ();
Static_assert <2 <3> ();

Currently, the implementation and prototype cannot be separated, so you can only put them in the same file, for example:

Template <class T>
Void fun ();

Template <class T>
Void fun (){...}

Or directly

Template <class T>
Void fun (){...}

I will directly give you an example, for example, write a square template:

// Fun. cpp

Template <class T>
T square (T x)
{
Return x * x;
}

// Main. cpp
# Include <iostream>

Template <class T>
T square (T );

Int main ()
{
Std: cout <square (2 );
}

Or

// Fun. h

Template <class T>
T square (T x) {return x * x ;}

// Main. cpp
# Include <iostream>
# Include "fun. h"

Int main ()
{
Std: cout <square (2 );
}

How to Use class templates in c ++

Template <typename T> // method template
T add (T const & a, const T & B) {// two const limits are equivalent
Return a + B;
} // Note that there is no semicolon here, because this is a method

Template <typename T> // structure template
Struct S {
T s;
}; // Note that there is a semicolon here, because this is a struct

Template <typename T> // class template
Class {
Public:
S <T> s; // The struct template is used.
Void set (T s0 );
}; // Note that there is a semicolon here, because this is a class

Template <typename T> // method in the implementation class
Void A <T >:: set (T s0) {// note the format of this row
S. s = s0;
}
// Note that this method will start to be instantiated only when it is used, but it can be explicitly instantiated so that it can be instantiated at the beginning.

Int main (){
Template A <int>; // explicitly instantiate
Template A <double>;
Int a = 1, B = 2;
Double c = 3.3;
Add (a, B); // correct
Add (a, c); // error. The template parameter type is strictly matched and cannot be implicitly converted.
A <int> a; // class template used
A. set (5 );
Cout <a. s; // output 5
}

The above is the type parameter Template
The following is a value parameter template.
Template <unsigned int N>
Class bitset;
...
Bitset <100> b1;

Template customization, that is, special processing for a certain type of parameters. For example, addition is not applicable to all types. For char * types, customization is required.
// Define the template first
Template <typename T>
Class {
Public:
T s;
Void show ();
}
Template <typename T>
Void A <T >:: show (){
Cout <s <endl;
}
// Customize the Template
Class B {}// define a type
Template <> // do not enter the type
Class A <B> {// directly use the type you need
Public:
B s; // the desired type.
Void show ();
}
Void A <B >:: show () {// note the format. There is no template statement.
Cout <s <endl;
}

Template parameters can have multiple
Template <typename T1, typename T2>
Class {}

Only customized T2, not customized T1, is called local custom, such:
Template <typename T1>
Class A <T1, T1> {}
Or
Template <typename T1>
Class A <...... the remaining full text>

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.