C++编程思想 第2卷 第5章 深入理解模板 模板参数 typename关键字

博客围绕C模板编程展开,指出处理的类T需有嵌套标识符id,id也可是T的静态数据成员,可直接操作。还提到模板中出现标识符时,若编译器在将其视为类型或其他元素间可选择,不会认为它是类型。

这个模板定义假定
处理的类T必须拥有某种称为id的嵌套标识符
id也可以是一个T的静态数据成员
这样就可以直接对id进行操作

//: C05:TypenamedID.cpp {-bor}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Uses 'typename' as a prefix for nested types.

template<class T> class X {
  // Without typename, you should get an error:
  typename T::id i;
public:
  void f() { i.g(); }
};

class Y {
public:
  class id {
  public:
    void g() {}
  };
};

int main() {
  X<Y> xy;
  xy.f();
} ///:~

无输出

当模板中出现一个标识符时
若编译器可以在把这个标识符当做一个类型
或把它当做一个除类型之外的其他元素之间选择的话
则编译器将不会认为这个标识符是一个类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值