member function template (template) in C + +

Source: Internet
Author: User
Tags sort

Membership templates (member template) can be either in the normal class (ordinary Class) or in the class template (class template);

In normal classes, when you use member functions, you do not provide template parameters, and functions can be based on the parameters used

Automatic derivation (deduce) template arguments (template argument) correspond to template parameters (template parameter);

In a class template, the template parameters (template parameter) of a member function can be different from the template parameters of a class, but in definition,

You must add two template parameter lists (template parameter list), the first is a class, and the second is a member function;

The code is as follows:

* * * cppprimer.cpp * * Created on:2013.11.24 * author:caroline//*eclipse CDT, gcc 4.8.1*/ #include <iostream> #include <functional> #include <memory> #include <algorithm> #incl Ude <string> #include <vector> #include <deque> #include <iterator>//function template default parameter Te   
    Mplate <typename T, typename f = std::less<t>> int compare (const t &AMP;V1, const T &v2, F f = f ()) {  
    if (f (V1, V2)) return-1;  
    if (f (V2, v1)) return 1;  
return 0; Class Debugdelete {public:debugdelete (std::ostream &s = std::cerr): OS (s) {} template &   
    Lt;typename t> void operator () (T *p) const {OS << "deleting unique_ptr" << Std::endl; Delete p;  
} Private:std::ostream &os;  
      
};  
    Template <typename t> class Blob {public:template <typename it> Blob (it B, it e); /*template &LT;typename it> Blob (it B, it e) {Std::sort (b, E);  
      
}*/
};  Template <typename t> template <typename it> Blob<t>::blob (it B, it e) {Std::sort (b, E); Container needs to be allowed to be sorted} int main (void) {std::cout << "compare (0) =" << Compare (0, 42)  
      
    << Std::endl;  
    double* p = new Double;  
    Debugdelete D; D (P);  
    When used, can automatically tear down the template int* IP = new int;  
    Debugdelete () (IP);  
    Std::unique_ptr<int, debugdelete> Pi (new int, debugdelete ());  
      
    Std::unique_ptr<std::string, debugdelete> PS (New std::string, Debugdelete ());  
    int ia[] = {9, 8, 7, 6, 5};  
    Std::vector<long> VI = {5, 4, 3, 2, 1, 0};  
      
    std::d eque<std::string> w = {"Lady", "Girl", "Woman", "Now"};  
    Blob<int> A1 (Std::begin (IA), Std::end (IA));  
    Blob<int> A2 (Vi.begin (), Vi.end ());  
      
    Blob<std::string> A3 (W.begin (), W.end ());Std::cout << "int ia[] =";  
    for (const auto I:ia) {std::cout << i << "";}  
    Std::cout << Std::endl;  
    Std::cout << "std::vector<long> vi =";  
    for (const auto I:VI) {std::cout << i << "";}  
    Std::cout << Std::endl;  
    Std::cout << "Std::list<const char*> w =";  
    for (const auto i:w) {std::cout << i << "";}  
      
    Std::cout << Std::endl;  
      
return 0; }

Output:

deleting unique_ptr  
deleting unique_ptr deleting unique_ptr deleting unique_ptr compare  
(0, 42) =-1  
int ia[] = 5 6 7 8 9   
std::vector<long> VI = 0 1 2 3 4   
std::list<const char*> w = girl lady's Now Woman

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.