16.17
当class和typename同在模版参数列表中时,两者并无不同(P580),但是当函数返回类型是模版内定义的类型成员时,必须使用typename,.用来区分是否是static成员
16.18
template
void f1(T,U,V);
template
T f2(T& i){
}
template inline T foo(T,unsigned int*);
d.缺少返回类型符
template void f4(T,T){
}
e.typedef char Ctype 会被忽略
typedef char Chartype;
template Ctype f5(Ctype a){
return a;
}
16.19
template
void printContainer(T & con){
for(typename T::size_type i = 0 ; i != con.size(); i++){
cout << con[i] << ends;
}
cout < vecI = { 1,2,3,4,5,6,7,8,9 };
vector vecC = { 'a','b','c','d' };
printContainer(vecI);
printContainer(vecC);
for (;;);
return 0;
}
16.20
template
void printContainerIterator(T & con) {
for (typename T::iterator it = con.begin(); it != con.end(); it++) {
cout << *it << ends;
}
cout << endl;
}
int main() {
vector vecI = { 1,2,3,4,5,6,7,8,9 };
vector vecC = { 'a','b','c','d' };
printContainerIterator(vecI);
printContainerIterator(vecC);
for (;;);
return 0;
}
16.21
class DebugDelete {
public:
DebugDelete(ostream& os = std::cerr) :os(os) {}
template
void operator()(T * p) {
os << "delete p " << endl;
delete p;
}
private:
ostream & os;
};
16.22
class QueryResult;
using line_no = vector::size_type;
class TextQuery {
public:
TextQuery(ifstream&);
TextQuery(const TextQuery& tq) :file(new StrVec(*tq.file), DebugDelete()) {
for (auto it : tq.wm) {
wm[it.first].reset(new set(it.second->begin(), it.second->end()), DebugDelete());
}
};
TextQuery(ifstream& ifs, const set setInt) :file(new StrVec()) {
string lineStr, word;
while (getline(ifs, lineStr))
{
file->push_back(lineStr);
int lineCount = file->size();
if(setInt.find(lineCount) == setInt.end()){
continue;
}
istringstream ist(lineStr);
while (ist >> word)
{
shared_ptr>& line = wm[word];
if (!line) {
line.reset(new set, DebugDelete());
}
line->insert(lineCount);
}
}
};
//int getSize()const { return file->size(); };
//void clear()const { file->clear(); };
QueryResult query(const string &)const;
private:
shared_ptr file;
map>> wm;
};
TextQuery::TextQuery(ifstream & ifs) :file(new StrVec(), DebugDelete()) {
string lineStr, word;
while (getline(ifs, lineStr))
{
file->push_back(lineStr);
int lineCount = file->size();
istringstream ist(lineStr);
while (ist >> word)
{
shared_ptr>& line = wm[word];
if (!line) {
line.reset(new set, DebugDelete());
}
line->insert(lineCount);
}
}
}
16.23
在智能指针释放的时候进行调用结果也是如此,作用域结束后释放指针时,调用了函数对象
16.24
template
template
Blob::Blob(BT begin, BT end) :data(make_shared>(begin, end)) {
}