这个模板定义假定
处理的类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();
} ///:~
无输出
当模板中出现一个标识符时
若编译器可以在把这个标识符当做一个类型
或把它当做一个除类型之外的其他元素之间选择的话
则编译器将不会认为这个标识符是一个类型