16.17 在模板参数列表中,typename和class含义相同,typename比class更直观,因为内置类型也可以作为模板类型实参。当使用作用域运算符(::)访问一个模板类型参数的成员时,该成员可能是静态成员,也可能是类型成员,为了区分这点,需要在名字前使用typename关键字,指明其是一个类型。
16.18 (a)非法。U是类型参数,必须加上typenmae关键字;(b)非法。在作用域中,模板参数不能重用,这里用T当作参数名使用,错误;(c)非法。模板在定义时才能使用inline;(d)非法。没有返回值;(e)合法,但是模板声明的Ctype隐藏了类型别名Ctype。
16.19
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void Print(const T &con)
{
for (typename T::size_type i = 0; i != con.size(); i++)
cout << con.at(i) << endl;
}
int main()
{
vector<int> ivec = {1, 2, 3, 5, 7, 11};
Print(ivec);
return 0;
}
16.20
#include <iostream>
#include <deque>
using namespace std;
template <typename T>
void Print(const T &con)
{
for (auto iter = con.begin(); iter != con.end(); ++iter)
cout << *iter << endl;
}
int main()
{
deque<string> s = {"s1", "s2", "s3"};
Print(s);
return 0;
}
16.21
class DebugDelete {
public:
DebugDelete(ostream &s = cerr): os(s) {}
template <typename T> void operator()(T *p) const
{ os << "deleting shared_ptr" << endl; delete p;}
private:
ostream &os;
};
16.22 对初始化TextQuery类的file成员做如下修改:
TextQuery::TextQuery(ifstream &infile): file(new StrBlob(), DebugDelete())
16.23 在程序结束时释放shared_ptr指向的资源,所以会在程序结束时看到打印语句。
16.24 在类模板中添加声明:
template <typename It> Blob(It, It);
在类模板外定义:
template <typename T>
template <typename It>
Blob<T>::Blob(It b, It e): data(make_shared<vector<T>>(b, e)) { }