细读《Effective C++》之十一
来源:互联网 发布:java接入微信公众平台 编辑:程序博客网 时间:2024/04/28 17:23
Chapter 7. Templates and Generic Programming
Scott在Item 01里面就列举出了C++中的四种所谓“sublanguages”:C、OO C++、Template C++、STL。前面的Items一直评论的是OO C++,这一章则主要针对Template C++。
Item 41 - 42
条款41:Understand implicit interfaces and compile-time polymorphism
在OO C++中,是使用explicit interface和runtime polymorphism解决polymorphism问题的,而Template C++则是通过implicit interface和compile-time polymorphism解决polymorphism问题:
class Widget {
public:
Widget();
virtual ~Widget();
virtual std::size_t size() const;
virtual void normalize();
void swap(Widget& other); // see Item 25
...
};
void doProcessing(Widget& w)
{
if (w.size() > 10 && w != someNastyWidget) {
Widget temp(w);
temp.normalize();
temp.swap(w);
}
}
// template C++
template<typename T>
void doProcessing(T& w)
{
if (w.size() > 10 && w != someNastyWidget) {
T temp(w);
temp.normalize();
temp.swap(w);
}
}
Implicit interfaces并不基于function signatures,从上面的valid expressions可以看出,w要支持size、normalize、swap、copy constructor、operator !=等implicit interfaces。由于operator overloading的因素,这些条件都不需要满足:size、normalize、swap可以从base class继承,copy constructor可以采用default,只要存在operator !=隐式转换类型即可满足。
Things to Remember
1) Both classes and templates support interfaces and polymorphism.
2) For classes, interfaces are explicit and centered on function signatures. Polymorphism occurs at runtime through virtual functions.
3) For template parameters, interfaces are implicit and based on valid expressions. Polymorphism occurs during compilation through template instantiation and function overloading resolution.
条款42:Understand the two meanings of typename
1、声明template type parameter:
template<typename T> class Widget; // uses "typename"
2、指涉nested dependent type name:
void print2nd(const C& container)
{
if (container.size() >= 2) {
typename C::const_iterator iter(container.begin());
...
}
}
template<typename C> // typename allowed (as is "class")
void f(const C& container, // typename not allowed
typename C::iterator iter); // typename required
template<typename T>
class Derived: public Base<T>::Nested { // base class list: typename not
public: // allowed
explicit Derived(int x)
: Base<T>::Nested(x) // base class identifier in mem
{ // init. list: typename not allowed
typename Base<T>::Nested temp; // use of nested dependent type
... // name not in a base class list or
} // as a base class identifier in a
... // mem. init. list: typename required
};
Things to Remember
1) When declaring template parameters, class and typename are interchangeable.
2) Use typename to identify nested dependent type names, except in base class lists or as a base class identifier in a member initialization list.
- 细读《Effective C++》之十一
- 细读《Effective C++》之十一
- 细读《Effective C++》之二
- 细读《Effective C++》之三
- 细读《Effective C++》之四
- 细读《Effective C++》之五
- 细读《Effective C++》之六
- 细读《Effective C++》之七
- 细读《Effective C++》之八
- 细读《Effective C++》之九
- 细读《Effective C++》之十
- 细读《Effective C++》之二
- 细读《Effective C++》之三
- 细读《Effective C++》之四
- 细读《Effective C++》之五
- 细读《Effective C++》之六
- 细读《Effective C++》之七
- 细读《Effective C++》之八
- Tomcat的类载入器的结构
- 结合Direct Web Remoting使用AJAX
- 工作态度面面关小结
- 世乒赛:王厉勤就是王厉勤,好样的。
- 安装JBOSS
- 细读《Effective C++》之十一
- 简易聊天工具FreeChat(C#.net 2003)
- 深入浅出Attribute (上)——Attribute初体验
- getch(),getche(),getchar()的区别
- 十字路口的痕迹
- 2006年5月27日IBM暑期实习笔试总结
- 解决JSP中使用request乱码问题
- JDOM使用详解及实例
- ftp客户端源代码,使用VC+SOCKET编程,在SERV-U6.0服务器上测试通过,能断点续传