C TemplateCornerCases计算机等级考试
文章作者 100test 发表时间 2010:01:01 12:14:50
来源 100Test.Com百考试题网
编辑特别推荐:
全国计算机等级考试(等考)指定教材
全国计算机等级考试学习视频
全国计算机等级考试网上辅导招生
全国计算机等级考试时间及科目预告
百考试题教育全国计算机等级考试在线测试平台
全国计算机等级考试资料下载
全国计算机等级考试论坛
Following are some corner cases of C template features. A lot of the text is simply extracted from "C Templates: The Complete Guide", with some of my personal understanding. These features are trivial and easily neglected, but you should have some impression to them in case you run into troubles caused by the neglect.
I made my notes in English, and I don t bother to translate them into Chinese, forgive my laziness.
0. use "typename" to extract type defined within a type parameter, like this: typename T::iterator itr. otherwise the itr is treated as a data member of T.
1. zero initialization
When using a var x of type T, we MUST initialize it like this: T x = T(). so that when T is a primitive type, x can also be initialized with 0, or NULL (when T is a pointer type). Of course we must make sure
when T is a class type, it has a default constructor. Simply using T x. can t initialize x when T is a primitive type.
2. .template
The .template Construct
A very similar problem was discovered after the introduction of typename. Consider the following example using the standard bitset type:
template