zoukankan      html  css  js  c++  java
  • c++ standard library II

    Template specialization
    If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template.
    
    For example, let's suppose that we have a very simple class called mycontainer that can store one element of any type and that it has just one member function called increase, 
    which increases its value. But we find that when it stores an element of type char it would be more convenient to have a completely different implementation with a function
    member uppercase, so we decide to declare a class template specialization for that type:
    Template specialization
    // template specialization
    #include <iostream>
    using namespace std;
    
    // class template:
    template <class T>
    class mycontainer {
        T element;
      public:
        mycontainer (T arg) {element=arg;}
        T increase () {return ++element;}
    };
    
    // class template specialization:
    template <>
    class mycontainer <char> {
        char element;
      public:
        mycontainer (char arg) {element=arg;}
        char uppercase ()
        {
          if ((element>='a')&&(element<='z'))
          element+='A'-'a';
          return element;
        }
    };
    
    int main () {
      mycontainer<int> myint (7);
      mycontainer<char> mychar ('j');
      cout << myint.increase() << endl;
      cout << mychar.uppercase() << endl;
      return 0;
    }

  • 相关阅读:
    批量插入以及数据存在重复就进行更新操作
    插件-过滤器
    NamedParameterJdbcTemplate
    菜鸟python---文件 + 操作
    菜鸟python---文件操作
    菜鸟python---以后会遇到的坑
    菜鸟python---二次编码
    菜鸟python---基础数据类型补充
    菜鸟python---深浅拷贝
    菜鸟python---集合
  • 原文地址:https://www.cnblogs.com/cjyp/p/11968570.html
Copyright © 2011-2022 走看看