原文地址http://blog.csdn.net/bug07250432/article/details/10150625 

 在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?
相信学习C++的人对class这个关键字都非常明白,class用于定义类,在模板引入c++后,最初定义模板的方法为: template<class T>...... 

  这里class关键字表明T是一个类型,后来为了避免class在这两个地方的使用可能给人带来混淆,所以引入了typename这个关键字,它的作用同
class一样表明后面的符号为一个类型,这样在定义模板的时候就可以使用下面的方式了: template<typenameT>......
在模板定义语法中关键字class与typename的作用完全一样。

typename难道仅仅在模板定义中起作用吗?其实不是这样,typename另外一个作用为:使用嵌套依赖类型(nested depended name),如下所示:
class MyArray 

public:
  typedef int LengthType;
  .....
}

template<class T>
void MyMethod( T myarr ) 

  typedef typename T::LengthType LengthType; 
  LengthType length = myarr.GetLength; 

  这个时候typename的作用就是告诉c++编译器,typename后面的字符串为一个类型名称,而不是成员函数或者成员变量,这个时候如果前面没有
typename,编译器没有任何办法知道T::LengthType是一个类型还是一个成员名称(静态数据成员或者静态函数),所以编译不能够通过。

<转载>模板声明中template <typename T>和template <class T>的更多相关文章

  1. C++中模板template <typename T>

    最近在看C++的源码,遇到了不少问题,一点一点进行补充. 首先就是遇到template <typename Dtype>. 网上解释的非常多,觉得比较啰嗦,其实就是一个类型模板. 比如我们 ...

  2. template <typename T>模板类定义

    #include "stdafx.h"#include "iostream"#include <ctime>using namespace std; ...

  3. c++ template<typename T>

    template <typename T> 网上查了半天不知所云,网上说的太多,俺只是要知道所需要的就可以了. 写了个程序试了一下,其实就是这个东西可以根据你所需要的类型就行匹配.其实就是 ...

  4. WPF - 模板查看工具:Show Me The Template及如何查看第三方主题

    原文:WPF - 模板查看工具:Show Me The Template及如何查看第三方主题 在学习WPF的模板(DataTemplate.ItemsPanelTemplate.ControlTemp ...

  5. C++ Templates (2.1 类模板Stack的实现 Implementation of Class Template Stack)

    返回完整目录 目录 2.1 类模板Stack的实现 Implementation of Class Template Stack 2.1.1 声明类模板 Declaration of Class Te ...

  6. Qt类声明中Q_OBJECT的作用与报错解决

    2017-06-22 周四 大雨 北京 院里 新建作图类,继承自QCUstomPlot类 因为需要同时作8张图,都要单坐标缩放的功能,因此想干脆新建一个类,继承自QCUstomPlot,把需要的功能都 ...

  7. Django——如何使用Template以及如何向template传递变量

    如何使用模板系统 在Python代码中使用Django模板的最基本方式如下: 可以用原始的模板代码字符串创建一个 Template 对象, Django同样支持用指定模板文件路径的方式来创建 Temp ...

  8. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  9. 【转载】#323 - A Generic Class is a Template for a Class

    A generic classs is a class that takes one or more type parameters, which it then uses in the defini ...

随机推荐

  1. yaml 1.6 操作

    /** * Copyright (c) 2008, http://www.snakeyaml.org * * Licensed under the Apache License, Version 2. ...

  2. 解决&nbsp在IE与firefox宽度不一致的问题

    浏览器默认不同的字体问题,字体分为“等宽”和“不等宽”字体,所以&nbsp在IE与firefox内间距是不等的.   解决办法:   body{font-family: 宋体, simsun; ...

  3. AVL旋转树

    执行插入操作可能出现不平衡的情况,当平衡二叉树.AVL这树是一种自平衡二叉树,使二叉树又一次保持平衡.而且查找.插入和删除操作在平均和最坏情况下时间复杂度都是O(log n) AVL树的旋转一共同拥有 ...

  4. JavaScript 字符串函数 之查找字符方法(一)

    1.JavaScript查找字符方法   首先看一张 有关字符串函数的查找字符的方法图 接下里补充里面的方法说明 2.charAt()     charAt() 方法返回字符串中指定位置的字符 语法 ...

  5. [转]MVP模式开发

    转自:http://www.jianshu.com/p/f7ff18ac1c31 基于面向协议MVP模式下的软件设计-(iOS篇) 字数9196 阅读505 评论3 喜欢11 基于面向协议MVP模式下 ...

  6. Power Designer - 反向获取数据库物理模型时Unable to list the users 异常

    解决方案: 菜单栏 -> Database -> Change Current DBMS ,给DBMS选择Oracle Version 9i2.

  7. PixelFormat 枚举

    成员名称 说明 Alpha 像素数据包含没有进行过自左乘的 alpha 值. Canonical 默认像素格式,每像素 32 位. 此格式指定 24 位颜色深度和一个 8 位 alpha 通道. Do ...

  8. 加载gif图过渡效果

    加载gif图片,过渡效果: 调用: - (id)initWithGifView:(UIView *)view { self = [super initWithView:view]; if (self) ...

  9. C/C++中的switch使用

    代码: #include <iostream> #include <string> #include <cstdio> using namespace std; i ...

  10. rr

        times = gcd(rotdist,length);  printf( ;i<times;i++) {  t = vec[i];   j = i;    ) {   k = j+ r ...