Some syntax for C + + templates

Source: Internet
Author: User

The specificity of function templates

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);}

The specificity of class templates

Template <class t>class test{public:void operator () () {cout<< "test<t>" <<endl;}}; Template <>  class test<char>{public:void operator () () {cout<< "test<char>" <<endl;}};
the partial specificity of the template

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 Templates

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 of templates to friends

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 templates

Inline template <typename t> t min (const t& a,const T & B) {return a<b?a:b;}
Note that the position of the inline and template is not interchangeable

Template internal specified type

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;};


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.