普通(非模板)类的成员模板

16.21 编写你自己的DebugDelete版本。

#include<iostream>
#include<new>
using namespace std;

class DebugDelete
{
public:
    DebugDelete(ostream &s=cerr):os(s) {}
    template <typename T>
    void operator()(T *p) const
    {
        os<<"deleting unique_ptr "<<endl;
        delete p;
    }
private:
    ostream &os;
};

int main()
{
    double *p=new double;
    DebugDelete d;
    d(p);
    int *ip=new int;
    DebugDelete()(ip);
    return 0;
}

16.22 修改TextQuery程序,令shared_ptr成员使用DebugDelete作为它们的删除器。

TextQuery.h

#ifndef TEXTQUERY_H
#define TEXTQUERY_H
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<memory>
#include<map>
#include<set>
#include<new>
#include"DebugDelete.h"
using namespace std;
class QueryResult;
class TextQuery
{
public:
    using line_no=vector<string>::size_type;
    TextQuery(ifstream&);
    QueryResult query(const string&) const;
    ~TextQuery()
    {
        //DebugDelete()(new vector<string>);
        cout<<"destructing...."<<endl;
    }
private:
    shared_ptr<vector<string>> file;
    map<string,shared_ptr<set<line_no>>> wm;
};
#endif // TEXTQUERY_H

TextQuery.cpp

#include"TextQuery.h"
#include"QueryResult.h"
#include<sstream>
TextQuery::TextQuery(ifstream& is):file(new vector<string>,DebugDelete())
{
    string text;
    while(getline(is,text))
    {
        file->push_back(text);
        int n=file->size()-1;
        string word;
        istringstream line(text);
        while(line>>word)
        {
            auto &lines=wm[word];
            if(!lines)
                lines.reset(new set<line_no>);
            lines->insert(n);
        }
    }
}

QueryResult TextQuery::query(const string& sought) const
{
    static shared_ptr<set<line_no>> nodata(new set<line_no>);
    auto loc=wm.find(sought);
    if(loc!=wm.end())
        return QueryResult(sought,loc->second,file);
    else
        return QueryResult(sought,nodata,file);
}

DebugDelete.h

#include<iostream>
#include<new>
using namespace std;

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;
};
时间: 2024-08-25 03:58:28

普通(非模板)类的成员模板的相关文章

类模板、模板类、函数模板、模板函数

一:什么是类模板 一个类模板允许用户为类定义一种模式,使得类中的某些数据成员.默认成员函数的参数.某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的) 如果一个类中数据成员的数据类型不能确定.或者是某个成员函数的参数或返回值的类型不能确定,就必须将此类声明为模板,它的存在不是代表一个具体的.实际的类,而是代表一类类. 二:类模板定义及注意事项 template<class 模板参数表> 或者template<typename 模板参数表> class 类名 { /

忍不住吐槽类模板、模板类、函数模板、模板函数

最近在查资料,发现了一些blog上写"类模板.模板类.函数模板.模板函数的区别"之类的文章.一看之下,闭起眼睛想想,自己写了这么久C++,知道模板,知道函数,也知道类.如果单独问我,类模板或者模板类,我都认为是采用了模板的类.但不知道这"类模板.模板类.函数模板.模板函数"是什么东西. 仔细看了下文章,忍不住吐槽了.其实就是采用模板的类叫类模板,实例化后叫模板类.采用模板的函数叫函数模板,实例化后叫模板函数.好吧,你赢了.知道模板的都会知道,模板实例化后会由编译器生

C++中模板类使用友元模板函数

在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定实例的访问权的友元声明. (1)普通友元: template<class T> class A{ friend void fun(); //... };此例中fun可访问A任意类实例中的私有和保护成员 (2)一般模板友元关系 template<class type> class A{

模板类的约束模板友元函数:template friend functions

本来这篇博客是不打算写的,内容不是很难,对于我自己来讲,更多的是为了突出细节. 所谓template friend functions,就是使友元函数本身成为模板.基本步骤:1,在类定义的前面声明每个模板函数.eg:template <typename T> void counts(); template <typename T> void report<>(T &);2,在类声明中再次将模板声明为友元. template <typename TT>

C++ Primer 学习笔记_45_模板(三):缺省模板参数(借助标准模板容器deque实现Stack模板)、成员模板、关键字typename

一.缺省模板参数 1.stack内存能否借助标准模板容器管理呢?答案是肯定的,只需要多传一个模板参数即可,而且模板参数还可以是缺省的,如下: template <typename T, typename CONT = std::deque<T> > //此处末尾必须有空格,否则编译出错 class Stack { - private: CONT c_; }; 如果没有传第二个参数,默认为deque 双端队列,当然我们也可以传递std::vector<T> 2.示例:借助

C++学习笔记(一)模板类的友元模板函数Boolan

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include<iostream> #include<string> using namespace std; template<class T> class Test; template<class T> ostream& operator<&l

数据结构与算 5:C++ 顺序/链式存储,栈 模板类实现,编译模板类问题解决

[本文谢绝转载原文来自http://990487026.blog.51cto.com] 数据结构与算 5:C++ 顺序/链式存储,栈 模板类实现 C++ 顺序存储模板类的实现[面试重要] C++ 链式存储模板类的实现[面试重要] C++ 链式存储模板类,载入指针 c++ 栈的链式存储实现(模板类嵌套模板类),编译问题教训[重要] C++ 顺序存储模板类的实现[面试重要] 项目文件: [email protected]://990487026.blog.51cto.com~/c++$ tree .

模板与泛型编程——定义模板

一.定义模板 1.函数模板 模板定义以关键字template开始,后跟一个模板参数列表,这是一个逗号分隔的一个或多个模板参数的列表,用<>括起来.在模板定义中,模板参数列表不能为空.模板参数表示在类或函数定义中用到的类型或值.当使用模板时,我们(隐式地或显式地)指定模板实参,将其绑定到模板参数上. 1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algo

函数模板与模板函数及模板类与模板的特化

函数模板( Function templates) * 模板(Templates)使得我们可以生成通用的函数,这些函数能够接受任意数据类型的参数,可返回任意类型的值,而不需要对所有可能的数据类型进行函数重载.这在一定程度上实现了宏(macro)的作用.它们的原型定义可以是下面两种中的任何一个: template <class identifier> function_declaration; template <typename identifier> function_decla