zoukankan      html  css  js  c++  java
  • 模板偏特化的威力

    模板偏特化的威力挺强大!

    我列举两项如下。

    第一是作为类型萃取器,萃取出真实类型。

     1 #ifndef TYPE_TRAITS_H
    2 #define TYPE_TRAITS_H
    3
    4 template<typename T>
    5 struct type_traits{
    6 typedef T value_type;
    7 };
    8
    9 template<typename T>
    10 struct type_traits<T*>{
    11 typedef T value_type;
    12 };
    13
    14 template<typename T>
    15 struct type_traits<const T>{
    16 typedef T value_type;
    17 };
    18
    19 template<typename T>
    20 struct type_traits<const T *>{
    21 typedef T value_type;
    22 };
    23
    24 #endif

    client.cpp

    #include "type_traits.h"

    #include <iostream>
    using namespace std;

    class Obj{};

    /**
    * 萃取相应类型
    */
    int main()
    {
    //---------------built-in type----------------
    cout << typeid(type_traits<int>::value_type).name() << endl;
    cout << typeid(type_traits<int*>::value_type).name() << endl;
    cout << typeid(type_traits<const int *>::value_type).name() << endl;

    //----------------UAD-------------------------
    cout << typeid(type_traits<Obj>::value_type).name() << endl;
    cout << typeid(type_traits<Obj *>::value_type).name() << endl;
    cout << typeid(type_traits<const Obj*>::value_type).name() << endl;


    return 0;
    }

    补充stl代码

    vector

     1 template <class T, class Alloc = alloc>
    2 class vector {
    3 public:
    4 typedef T value_type;
    5 typedef value_type* pointer;
    6 typedef value_type* iterator;
    7 typedef value_type& reference;
    8 typedef size_t size_type;
    9 typedef ptrdiff_t difference_type;
    10
    11 protected:
    12 //...
    13
    14 public:
    15 iterator begin(); //用萃取出的类型表示返回类型,perfect
    16 size_type size();
    17 //...
    18 reference front();
    19 //......
    20 };

     


    第二是实现Tuple,此Tuple可以传入任意数量的参数。此例中仅仅仅仅实现传入4、3、2个参数。

     1 //tuple.h
    2 #ifndef TUPLE_H
    3 #define TUPLE_H
    4
    5 /**
    6 * tuple.h
    7 * by Harry Sun
    8 * 2011-11-28
    9 *
    10 */
    11
    12 namespace tuple{
    13
    14 /**
    15 * Four tuples
    16 */
    17 template<typename A, typename B, typename C=void, typename D=void>
    18 class Tuple{
    19 private:
    20 A a;
    21 B b;
    22 C c;
    23 D d;
    24 public:
    25 Tuple();
    26 Tuple(A a, B b, C c, D d){
    27 this->a = a;
    28 this->b = b;
    29 this->c = c;
    30 this->d = d;
    31 }
    32 };
    33
    34 /**
    35 * Three tuples
    36 */
    37 template<typename A, typename B, typename C>
    38 class Tuple<A, B, C, void>{
    39 private:
    40 A a;
    41 B b;
    42 C c;
    43 public:
    44 Tuple();
    45 Tuple(A a, B b, C c){
    46 this->a = a;
    47 this->b = b;
    48 this->c = c;
    49 }
    50 };
    51
    52 /**
    53 * Two tuples
    54 */
    55 template<typename A, typename B>
    56 class Tuple<A, B, void, void>{
    57 private:
    58 A a;
    59 B b;
    60 public:
    61 Tuple();
    62 Tuple(A a, B b){
    63 this->a = a;
    64 this->b = b;
    65 }
    66 };
    67
    68 }
    69 #endif

    客户端代码

     1 #include "tuple.h"
    2 #include<string>
    3
    4 int main()
    5 {
    6 tuple::Tuple<int, double, char, std::string> tuple4(3, 2.0, 'c', "hello");
    7
    8 tuple::Tuple<int, double, char> tuple3(1, 2.0, 'd');
    9
    10 tuple::Tuple<int, int> pair(1, 2);
    11
    12 return 0;
    13 }

    可以传入4个、3个或者2个参数。

  • 相关阅读:
    分解质因数算法
    js 的 Math 对象
    字符串操作
    简化求质数算法
    数值类型小数点后是否可以接零问题
    新博第一篇,思考的重要性与求质数算法
    一、制作屏幕录像
    四、同步线程
    常见问题
    jni数据处理
  • 原文地址:https://www.cnblogs.com/harrysun/p/harrysun_cpp_template01.html
Copyright © 2011-2022 走看看