c++||template

  模版作为类或函数的蓝图公式,通过定义产生任意数量特定类型的类,特定的类型体现多态性.

//template
#ifndef templateDemo_H
#define templateDemo_H
#include<iostream>

using namespace std;

template<class type>
int  compare(const T &v1,const T &v2 ){
    if(v1>v2){
        return 1;
    }else
  return -1;

} #endif

 

  1.模版形参表

    函数形参表定义了特定类型的局部变量但不初始化那些变量,在运行时提供实参来初始化形参.模版形参表示类型形参(type parameter),也可以表示非类型形参(nontype parameter),在使用函数模版时,编译器会判断哪个模版实参(template parameter)绑定到模版形参进行实例化.

  2inline version

 template <typename T> inline T min(const T&,const T&);

  

  3.typename 和class区别

    在形参表中具有相同含义可以同时使用,没有区别,typename在模版内部使用类中定义的类型成员,告知该成员当作类型

   

  4.模版编译模型

    模版进行实例化需要编译器能够访问源代码,定义了2种模型,一种为包涵另一种为分别编译

   4.1包含编译模型

    通过#include引入包含文件

   4.2分别编译模型(separate compilation model)

    跟踪相关的模版定义,使用export关键字来声明,

 

      

 

1

posted @ 2017-10-13 20:36  OKSSJ  阅读(104)  评论(0编辑  收藏  举报