标签:style blog http color 使用 2014
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
经验:声明 template 参数时,前缀关键字 class 和 typename 可互换。请使用关键字 typename 标识嵌套从属类型名称;
示例1:
template<typename C>
void print2nd(const C &container){
C::const_iterator *x;//歧义。如果const_iterator是个static成员变量,x是个global 变量,这里的 *就是乘
//...
}template<typename C>
void print2nd(const C &container){
if(container.size() >= 2){
C::const_iterator iter(container.begin());//这个名称被假设为非类型
}
}template<typename C>
void print2nd(const C &container){
if(container.size() >= 2){
typename C::const_iterator iter(container.begin());
}
}template<typename T>
class Derived: public Base<T>::Nested{ //其实我觉得,用 typename 是为了让编译器知道嵌套从属类型名称是类型,而这里能用来继承的当然只能是类型了,所以就不用 typename 了
public:
explicit Derived(int x): Base<T>::Nested(x) // mem.init.list中不允许"typename"
{
typename Base<T>::Nested temp; //这里需要 typename
}
};Effective C++ Item 42 了解 typename 的双重意义,布布扣,bubuko.com
Effective C++ Item 42 了解 typename 的双重意义
标签:style blog http color 使用 2014
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37754685