终于无聊到来写书评,最近的项目一直都没和C++有什么关系,不过看的书却都是C++方面的,而最近看到的几本书中感觉最好的莫过于这本《C++ Templates》
Nicolai M. Josuttis的书我很喜欢,从他的那本《The C++ Standard Template Library》就看出了他很多独特的风格,令我爱不释手,所以这本《C++ Template》 也进入了我的必看书单。粗读之后,感觉整本书绝对将成为C++泛型领域的圣经级著作
- 这本书角度选得很好,全书分三个部分,分别介绍模板基础、模版的编译器实现、模板的高级技巧,三个部分相辅相成、相互照应,由浅入深而又自然而然,还方便分开阅读(比如我就重点看了第一第三部分,模版实现被我略过了)却又全面覆盖了这一领域
- 这本书英文很浅显(比《Modern C++ Design》浅显了不知多少倍),语言严谨而又不晦涩,尤其要赞的就是废话尤其地少!
- 章节安排很合理,很方别作为工具书应急查阅(《C++STL》就有这个优点,与这本书科学家+工程师的组合不无关系)
- 书中好多技术,我是闻所未闻,惊为天人,尤其第三部分,可以算得上眼花缭乱,而且给出的实现感觉既符合标准、实用、而且没有炫技的成分
同类书籍据我所知没有可以达到这个高度的,大部分C++泛型方面的专著只局限于怎么用STL,将模板基础的书,也仅限于最表面的语法,像模版参数推导这种问题鲜有涉及,更不用提关于Metaprogramming,这本书圣经的地位估计后人也是难以企及了。
下面是我看书时画下来的一些觉得自己平时应该注意的地方,放在这里做备忘好了
- (P12) [Argument Deducion] If we pass two ints to the parameter type T const& the C++ compiler must conclude that T must be int. Note that no automatic type conversion is allowed here,Each T must match exactly.
template <typename T>
inline T const& max (T const& a,T const& b);
max(4,7)//OK:T is int for both arguments
max(4,4.2)//ERROR:first T is int,second T is double
- (P13)[Template Parameters] In function templates(unlike class template) no default template arguments can be specified
- (P14)[Template Parameters]Deducation can be seen as part of overlaod resolution-a process tha is not based on selection of return type either.The sole exception is the return type of conversion operator members.
- (P18)[Overloading Function Template] The fact that not all overloaded functions are visible when a corresponding function call is made may or may not matter.
- (P39)[Nontype Function Template Parameters] Function templates are considered to name a set of overloaded function.However,according to the current standard,sets of overload functions cannot be used for template parameter deducation.Thus you have to cast to the exactly type of the function template arguments
template <typename T,int VAL>
T addValue (T const& x)
{
return x+VAL
}
std::transform(source.begin(),source.end(),//start and end of source
dest.begin(),//start of destination
(int(*)(int const&))addValue<int,5>);//operation
- (P40)[Restrictions for Nontype Template Parameters] 太长了,略过
- (P44)[The .template Construct]
template <int N>
void printBitset (std::bitset<N> const& bs)
{
std::cout<<bs.to_string<char,char_traits<char>,allacator<char> >();//ERROR:can't recogonize the template
}
template <int N>
void printBitset (std::bitset<N> const& bs)
{
std::cout<<bs.template to_string<char,char_traits<char>,allacator<char> >();//OK
}
- (P45)[Using this->]
template <typename T>
class Base
{
public:
void bar();
};
template <typename T>
class Derived : Base<T>
{
public:
void foo()
{
bar();//call external bar() or error
}
}
template <typename T>
class Derived : Base<T>
{
public:
void foo()
{
this->bar();//OK
}
}
- 同样精彩的还有(P57)[Using String Literals as Arguments for Function Templates]
- 令我惊异的SFINE技术(substitution-failure-is-not-an-error)
template <typename T>
class IsClassT
{
private:
typedef char One;
typedef struct {char a[2];} Two;
template <typename C> static One test (int::C*);
template <typename C> static Two test();
public:
enum {Yes=sizeof(IsClassT<T>::test<T>(0))==1};
enum {No=!Yes};
};
总而言之,此书带给了我前所未有的阅读享受......我今年震撼大奖一定会投它一票