精华内容
参与话题
问答
  • Templates

    2012-08-23 11:44:37
    Function templates Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one typ

    Function templates

    Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

    大致含义:功能模版是能操作一般类型的特殊函数。这样我们可以构建一种函数,能适合不止一种类型或类,而不需要重复每种类型的整个代码。


    1、The format for declaring function templates with type parameters is:

    声明待参数函数模版的格式如下:

    template <class identifier> function_declaration;
    template <typename identifier> function_declaration;


    The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.

    大致含义:上面两种格式唯一不同的是关键字,一个用的是class,一个是typename,他们表达差不多是相同的意思和相同的方式。

    template <class myType>
    myType GetMax (myType a, myType b) {
     return (a>b?a:b);
    }

    Here we have created a template function with myType as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. As you can see, the function template GetMax returns the greater of two parameters of this still-undefined type.

    这里我们创建了一个函数模版,myTpye作为模版参数。这个模版参数代表的类型没有指定(比如可以使int、char等),但是在模版函数中认为它好像是指定的。它的返回就是两个中比较大一个没有指定类型的值。

    2、To use this function template we use the following format for the function call:

    使用这个模版函数,我们用的调用方法格式如下:

    function_name <type> (parameters);

    For example

    int x,y;
    GetMax <int> (x,y)

    1>Here is the entire example:

    // function template
    #include <iostream>
    using namespace std;
    
    template <class T>
    T GetMax (T a, T b) {
      T result;
      result = (a>b)? a : b;
      return (result);
    }
    
    int main () {
      int i=5, j=6, k;
      long l=10, m=5, n;
      k=GetMax<int>(i,j);
      n=GetMax<long>(l,m);
      cout << k << endl;
      cout << n << endl;
      return 0;
    }

    Since both i and j are of type int, and the compiler can automatically find out that the template parameter can only be int. This implicit method produces exactly the same result:

    因为i和j都是int型的,编译器就自动的认为模版中参数是只能是int,产生同样的结果:

    2>

    // function template II
    #include <iostream>
    using namespace std;
    
    template <class T>
    T GetMax (T a, T b) {
      return (a>b?a:b);
    }
    
    int main () {
      int i=5, j=6, k;
      long l=10, m=5, n;
      k=GetMax(i,j);
      n=GetMax(l,m);
      cout << k << endl;
      cout << n << endl;
      return 0;
    }
    3>

    We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example:
    我们还能定义一个函数模版,能含有大于一个参数类型的,只需在<>指明多个类型的参数,如下:

    template <class T, class U>
    T GetMin (T a, U b) {
      return (a<b?a:b);
    }

    int i,j;
    long l;
    i = GetMin<int,long> (j,l);

    等价与

    i = GetMin (j,l);

    Class templates

    We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example: 

    我们也可以写类模版,一个类可以有多个成员使用模版参数作为类型,如下:

    template <class T>
    class mypair {
        T values [2];
      public:
        mypair (T first, T second)
        {
          values[0]=first; values[1]=second;
        }
    };

    mypair<int> myobject (115, 36);
    mypair<double> myfloats (3.0, 2.18);

    The only member function in the previous class template has been defined inline within the class declaration itself. In case that we define a function member outside the declaration of the class template, we must always precede that definition with the template <...> prefix:
    那个唯一个成员函数在类中已经定义了,这样的话,我们定义在类外定义它要声明类模版,在前面加上template<>:

    // class templates
    #include <iostream>
    using namespace std;
    
    template <class T>
    class mypair {
        T a, b;
      public:
        mypair (T first, T second)
          {a=first; b=second;}
        T getmax ();
    };
    
    template <class T>
    T mypair<T>::getmax ()
    {
      T retval;
      retval = a>b? a : b;
      return retval;
    }
    
    int main () {
      mypair <int> myobject (100, 75);
      cout << myobject.getmax();
      return 0;
    }

    template <class T>
    T mypair<T>::getmax ()

    红色标记中的T:第一个是模版参数,第二个是函数返回的类型,第三个是这个函数模版参数和同样是类模版参数。

    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.
    如果定义一个不同的实现对于一个模版,当我们可以为它定义一个特殊的模版。

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

    This is the syntax used in the class template specialization:
    这是那个特殊模版:
    template <> class mycontainer <char> { ... };

    Non-type parameters for templates
    Besides the template arguments that are preceded by the class or typename keywords , which represent types, templates can also have regular typed parameters, similar to those found in functions. As an example, have a look at this class template that is used to contain sequences of elements:
    包括模版参数(前提关键字是class or typename),就一个例子:
    // sequence template
    #include <iostream>
    using namespace std;
    
    template <class T, int N>
    class mysequence {
        T memblock [N];
      public:
        void setmember (int x, T value);
        T getmember (int x);
    };
    
    template <class T, int N>
    void mysequence<T,N>::setmember (int x, T value) {
      memblock[x]=value;
    }
    
    template <class T, int N>
    T mysequence<T,N>::getmember (int x) {
      return memblock[x];
    }
    
    int main () {
      mysequence <int,5> myints;
      mysequence <double,5> myfloats;
      myints.setmember (0,100);
      myfloats.setmember (3,3.1416);
      cout << myints.getmember(0) << '\n';
      cout << myfloats.getmember(3) << '\n';
      return 0;
    }

    It is also possible to set default values or types for class template parameters. For example, if the previous class template definition had been:
    这个也可以设置默认的值或类型给类模版参数,如下:
    template <class T=char, int N=10> class mysequence {..};

    可以用
    mysequence<> myseq;
    等价于:
    mysequence<char,10> myseq;



    展开全文
  • Shareable Retro Templates

    2020-11-24 12:51:07
    <div><p>This introduces sharable templates. <h3>Scopes <p>Now, a template has a "scope" which can be - team scope (the template is only visible to your team) - org scope (the template is only ...
  • <div><p>This PR introduces <strong>new templates structure</strong>. If you don't like it, please say it NOW. <ul><li>easy sharing of templates between product families (using multiple values in ...
  • Templates are not installed

    2020-11-30 11:48:43
    <div><p>I believe that we should ship and install a base set of templates that at least provide a starting point. <p>Currently the templates included here are not installed by default. Modification to...
  • println( len( t.Templates() ) ) } </code></pre> <p>The result of this code depends on whether you imported <code>text/template</code> or <code>...
  • policy_templates.zip

    2017-09-09 16:14:16
    先下载Zip file of ADM/ADMX templates and documentation。(需爬楼梯,可直接墙内下载:chrome.adm)把policy_templates.zip下载之后,解压并找到windows\adm\zh-CN的chrome.adm。 运行中输入“gpedit.msc” ,打开...
  • preview-templates" folders in the root of the projects, the same way as "build-templates" in project roots override the "resources\static\build-templates" default build templates. ...
  • Templates are among the most powerful features of C++, but they remain misunderstood and underutilized, even as the C++ language and development community have advanced. In C++ Templates, Second Editi...
  • target manifest-templates

    2020-11-24 20:03:57
    <ol><li>Move files from manifests/content to templates directory</li><li>Create new asset called templates that the target manifest-templates can directly call</li><li>All template files are separate ...
  • 压缩包是两本书,《C++Templates中文版》的简体字版和繁体字版,繁体字版是高清的,简体字版目前都是扫描的。 《C++Templates中文版》是C++模板编程的完全指南 旨在通过基本概念 常用技巧和应用实例三方面的有用资料...
  • fileTemplates

    2017-12-28 11:18:04
    使用在idea中Java后台使用的三层代码Template, 拷贝到项目中,在项目设置/File and Code Templates中可以查看,新建时可以引用
  • <div><p>As the NativeScript 2.5 release, NativeScript templates now <em>must</em> provide a complete <code>App_Resources</code> folder in order to work. I believe we should revert that change, and ...

空空如也

1 2 3 4 5 ... 20
收藏数 38,642
精华内容 15,456
关键字:

templates