C++编程思想 第2卷 第5章 深入理解模板 模板参数 成员模板

成员模板可以是类
不一定必须是函数
外部类模板内的成员类模板

//: C05:MemberClass.cpp
// 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.
// A member class template.
#include <iostream>
#include <typeinfo>
using namespace std;

template<class T> class Outer {
public:
  template<class R> class Inner {
  public:
    void f();
  };
};

template<class T> template<class R>
void Outer<T>::Inner<R>::f() {
  cout << "Outer == " << typeid(T).name() << endl;
  cout << "Inner == " << typeid(R).name() << endl;
  cout << "Full Inner == " << typeid(*this).name() << endl;
}

int main() {
  Outer<int>::Inner<bool> inner;
  inner.f();
  getchar();
} ///:~


输出
Outer == int
Inner == bool
Full Inner == class Outer<int>::Inner<bool>

typeId运算符
只有一个参数并返回一个type_info对象
这个对象的name()函数生成一个表示参数类型的字符串
typeId运算符也可以用一个表达式做参数
返回一个代表这个表达式类型的type_info对象

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82083643