zoukankan      html  css  js  c++  java
  • C++ template一些体悟(3)

    其实没啥体悟,因为还没有感受到这些例子的作用,记一下先

     1 #include <iostream>
     2 
     3 using namespace std;
     4 class alloc {
     5 
     6 };
     7 
     8 template<class T, class Alloc = alloc>
     9 class vector {
    10 public:
    11     void swap(vector<T, Alloc>&) {
    12         cout << "swap()" << endl;
    13     }
    14 };
    15 
    16 template<class T,class Alloc>
    17 inline void swap(vector<T, Alloc>& x,vector<T, Alloc>& y){
    18     x.swap(y);
    19 }
    20 
    21 
    22 int main(int argc, char **argv) {
    23     vector<int> x,y;
    24     swap(x,y);
    25     return 0;
    26 }

     以下用于验证class template内能否在有template

     1 #include <iostream>
     2 
     3 using namespace std;
     4 class alloc {
     5 
     6 };
     7 
     8 template<class T, class Alloc = alloc>
     9 class vector {
    10 public:
    11     typedef T value_type;
    12     typedef value_type* iterator;
    13   //测试class template内能否再有template
    14     template<class I>
    15     void insert(iterator position, I first, I last) {
    16         cout << "insert()" << endl;
    17     }
    18 };
    19 
    20 int main(int argc, char **argv) {
    21     int ia[5] = { 0, 1, 2, 3, 4 };
    22 
    23     vector<int> x;
    24     vector<int>::iterator ite;
    25     x.insert(ite, ia, ia + 5);
    26     return 0;
    27 }

    测试template参数可否根据前一个template参数而设定默认值

     1 #include <iostream>
     2 #include <cstddef>
     3 
     4 using namespace std;
     5 
     6 class alloc {
     7 
     8 };
     9 
    10 template<class T, class Alloc = alloc, size_t BufSiz = 0>
    11 class deque {
    12 public:
    13     deque() {
    14         cout << "deque" << endl;
    15     }
    16 };
    17 
    18 template<class T, class Sequence = deque<T>>
    19 class stack {
    20 public:
    21     stack() {
    22         cout << "stack" << endl;
    23     }
    24 private:
    25     Sequence c;
    26 };
    27 
    28 int main(int argc, char **argv) {
    29     stack<int> x;
    30     return 0;
    31 }

    运行结果:

    deque
    stack
    

     先成员变量构造,再构造函数。

    //注释说测试class template可否拥有non-type template参数

    //但是我没找到哪个是non-type

     1 #include <iostream>
     2 #include <cstddef>
     3 
     4 using namespace std;
     5 
     6 class alloc {
     7 
     8 };
     9 
    10 inline size_t __deque_buf_size(size_t n, size_t sz) {
    11     return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1));
    12 }
    13 
    14 template<class T, class Ref, class Ptr, size_t BufSiz>
    15 struct __deque_iterator {
    16     typedef __deque_iterator <T, T&, T*, BufSiz> iterator;
    17     typedef __deque_iterator <T, const T&, const T*, BufSiz> const_iterator;
    18     static size_t buffer_size() {
    19         return __deque_buf_size(BufSiz, sizeof(T));
    20     }
    21 };
    22 
    23 template<class T, class Alloc = alloc, size_t BufSiz = 0>
    24 class deque {
    25 public:
    26     typedef __deque_iterator <T, T&, T*, BufSiz> iterator;
    27 };
    28 
    29 int main(int argc, char **argv) {
    30     cout << deque<int>::iterator::buffer_size() << endl;
    31     cout << deque<int, alloc, 64>::iterator::buffer_size() << endl;
    32     return 0;
    33 }

    运行结果

    128
    64
    

     看过完整的STL代码,只觉得这样写很牛逼,但是不知道为什么要这样写。

    查了下non-type原来指BufSize。但其实还是没弄懂定义的iterator有什么优秀之处

    //bound friend templates 等着找到使用的地方

     1 #include <iostream>
     2 #include <cstddef>
     3 
     4 using namespace std;
     5 
     6 class alloc {
     7 
     8 };
     9 
    10 template<class T, class Alloc = alloc, size_t BufSiz = 0>
    11 class deque {
    12 public:
    13     deque() {
    14         cout << "deque" << ' ';
    15     }
    16 };
    17 
    18 template<class T, class Sequence>
    19 class stack;
    20 
    21 template<class T, class Sequence>
    22 bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y);
    23 
    24 template<class T, class Sequence>
    25 bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>&y);
    26 
    27 template<class T, class Sequence = deque<T>>
    28 class stack {
    29     //这个可以
    30     /*
    31      friend bool operator==<T>(const stack<T, Sequence>& x,
    32      const stack<T, Sequence>& y);
    33      friend bool operator< <T>(const stack<T, Sequence>& x,
    34      const stack<T, Sequence>& y);
    35      */
    36     //这个不可以
    37     /*
    38      friend bool operator==(const stack<T, Sequence>& x,
    39      const stack<T, Sequence>& y);
    40      friend bool operator<(const stack<T, Sequence>& x,
    41      const stack<T, Sequence>& y);
    42      */
    43     //操作符后不能去掉<>
    44     //参数的<>可以去掉
    45     friend bool operator==<T, Sequence>(const stack& x,
    46             const stack<T, Sequence>& y);
    47     friend bool operator< <>(const stack<T>& x, const stack<T, Sequence>& y);
    48 public:
    49     stack() {
    50         cout << "stack" << endl;
    51     }
    52 private:
    53     Sequence c;
    54 };
    55 
    56 template<class T, class Sequence>
    57 bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {
    58     cout << "operator==" << '	';
    59     return 1;
    60 }
    61 
    62 template<class T, class Sequence>
    63 bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>&y) {
    64     cout << "operator<" << '	';
    65     return 1;
    66 }
    67 
    68 int main(int argc, char **argv) {
    69     stack<int> x;
    70     stack<int> y;
    71 
    72     cout << (x == y) << endl;
    73     cout << (x < y) << endl;
    74     return 0;
    75 }

    explicit specialization(显示特化)

    还有个偏特化,可以了解一下

    C++模板之特化与偏特化详解

    原来偏特化就是C++ template一些体悟(2)写的东西,换了个名字,那里叫特殊设计

    还涉及到 A a();是声明一个函数而非对象的问题,引以为戒

     1 #include <iostream>
     2 #include <cstddef>
     3 
     4 using namespace std;
     5 
     6 #define __STL_TEMPLATE_NULL template<>
     7 
     8 template<class Key>
     9 class Hash {
    10 public:
    11     Hash() {
    12         cout << "Construct Hash<T>" << endl;
    13     }
    14 
    15     void operator()() {
    16         cout << "Hash<T>" << endl;
    17     }
    18 };
    19 
    20 //explicit specialization
    21 __STL_TEMPLATE_NULL
    22 class Hash<char> {
    23 public:
    24     Hash() {
    25         cout << "Construct Hash<char>" << endl;
    26     }
    27     void operator()() {
    28         cout << "Hash<char" << endl;
    29     }
    30 };
    31 
    32 int main(int argc, char **argv) {
    33 //    Hash<long> obj1(); //声明了一个函数而非对象
    34     Hash<long> obj1;
    35     Hash<char> obj2;
    36     obj1();
    37     obj2();
    38     return 0;
    39 }

    运行结果

    Construct Hash<T>
    Construct Hash<char>
    Hash<T>
    Hash<char
    
    少壮不识cpp,老大方知cpp可怕
  • 相关阅读:
    CentOS 6.5下快速搭建ftp服务器
    Ubuntu增加swap交换空间的步骤
    mysql官方下载安装教程(centos)
    阿里云上遇到: virtual memory exhausted: Cannot allocate memory
    解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
    centos安装nodejs和配置npm
    JavaScript(二)-精简
    JavaScript(一)
    ease,seae-in,ease-in-out,ease-out区别
    安装 sass 文档
  • 原文地址:https://www.cnblogs.com/Jacket-K/p/9264222.html
Copyright © 2011-2022 走看看