CG@CPPBLOG

/*=========================================*/
随笔 - 76, 文章 - 39, 评论 - 137, 引用 - 0
数据加载中……

《C++设计新思维》读书笔记(七)


 
2.2 模板偏特化

模板偏特化让你在template的所有可能实体中特化出一组子集。如:
1 
2 template < class Window, class Controller>
3 class Widget{};


你可以这样加以特化:

1 template <>
2 class Widget<ModalDialog, MyController>
3 {};


其中,ModalDialog 和 MyController 是另外定义的类。
有时候,你需要针对任意window并搭配一个特定的MyController来特化Widget,这时候就需要模板偏特化。

1 template < class Window >
2 class Widget < Window, MyController>
3 {};


还可以拿任意Button来偏特化:

1 template <class ButtonArg>
2 class Widget<Button<ButtonArg>, MyController>
3 {};


编译器会尝试找出最匹配的定义。但这样的机制不能用在函数身上,无论是成员函数还是非成员函数。

posted on 2007-12-17 22:12 cuigang 阅读(264) 评论(0)  编辑 收藏 引用 所属分类: 《C++设计新思维》读书笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理