effective c++ notes

chapter 7 模板与泛型编程


item 41

对于模板参数,接口是隐式的,只有在实例化时,才会去检查实际模板类型是否支持模板中的接口

item 42

template <typename C>
void print2nd(const C& container) {
    C::const_iterator* x;
}

如上代码,如果不给const::iterator加前缀typename,编译器是不会假设它是类型的,这样就会产生歧义。说不定const_iterator是某个类的静态变量呢,结果导致解释成与x相乘

不过,typename只能用于nested dependent type,如

template <typename C>
void f(const C& container, typename C::iterator iter);

C是自动推导的,只有iter需要标注它是type

猜你喜欢

转载自blog.51cto.com/baiwfg2/2147510