条款42:了解 typename 的双重含义
Understand the two meanings of typename.
声明 template 参数时,前缀关键字 class 和 typename 是没有区别的:
必须使用 typename 的时候
从属名称(dependent names):template 内相依赖于某个 template 参数;
嵌套从属名称(nested dependent name):从属名称在 class 内呈嵌套状;
嵌套从属名称有可能导致解析困难,因为 C::const_iterator
可能不是个类型,它有可能是位于 C 里面的一个 static 变量或是一个全局变量。所以在 template 中涉及一个嵌套从属类型名称时,必须在紧邻它的前一个位置放上关键字 typename。不过 typename 不可以出现在 base classes list 内的嵌套从属类型名称之前,也不可再 member initialization list 中作为 base class 修饰符:
Last updated
Was this helpful?