美文网首页
用例子解释模板特化

用例子解释模板特化

作者: YuWenHaiBo | 来源:发表于2018-06-05 20:59 被阅读10次

    关于什么是特化什么是偏特化这个概念之前一直不理解这样有什么好处,一直到工作碰到一些前辈写的一些代码才深深感受到c++模板特化的精髓。

    
    using namespace rapidjson;
    using namespace std;
    
    typedef rapidjson::Document json_dom_t;
    typedef rapidjson::Value    json_value_t;
    typedef rapidjson::Value::ConstMemberIterator json_value_item;
    typedef rapidjson::SizeType  json_size_type;
    typedef rapidjson::Document::AllocatorType json_allocator;
    typedef rapidjson::StringBuffer  json_stringbuf;
    
    
    template<typename T>
    inline void write_value(Writer<StringBuffer>&writer,T& field)
    {
        writer.StartObject();
        field.Encode(writer);
        writer.EndObject();
    }
    
    
    template<>
    inline void write_value<int>(Writer<StringBuffer>& writer,int &field)
    {
        writer.Int(field);
    }
    
    template <>
    inline void write_value<double>(Writer<StringBuffer>&writer,double &field)
    {
        writer.Double(field);
    }
    
    template<>
    inline void write_value<bool>(Writer<StringBuffer>&writer,bool &field)
    {
        writer.Bool(field);
    }
    
    template<>
    inline void write_value<std::string>(Writer<StringBuffer>&writer,std::string &field)
    {
        writer.String(field.c_str(),(SizeType)field.length());
    }
    template<typename T>
    inline void JSON_ENCODE_FIELD(Writer<StringBuffer>&writer,const char* name,T &field)
    {
        writer.String(name);
        write_value(writer,field);
    }
    
    template<typename T>
    inline void JSON_ENCODE_FIELD(Writer<StringBuffer>&writer,const char* name,std::vector<T> &field)
    {
        writer.String(name);
        writer.StartArray();
        for(typename std::vector<T>::iterator iter=field.begin();iter!=field.end();iter++)
        {
            write_value(writer,*iter);
        }
        writer.EndArray();
    }
    

    上诉代码是我见到一个项目封装的rapidjson,这里write_value用到了模板的特化有什么好处呢?我们分析一下这个代码就知道了。
    首先我们定义一个类

    class SCRHead 
    {
    
    public:
        // id
        string id;
        // 功能码
        string func;
        // 时间
        string time;
    void Encode(Writer<StringBuffer> &writer)
        {
            JSON_ENCODE_FIELD(writer, "id", id);
            JSON_ENCODE_FIELD(writer, "func", func);
            JSON_ENCODE_FIELD(writer, "time", time);
        };
    
    string Encode()
    {
        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        writer.StartObject(); // 产生了{
        JSON_ENCODE_FIELD(writer, "head", head);  //"head":{"id":"123456","func":"alarming_request","time":""}
        writer.EndObject(); // 产生了}
        return buffer.GetString();
    }
    }
    

    当我们运行下面代码

    int main() {
        SCRHead request;
        request.id = "123456";
        request.func = "alarming_request";
        std::cout << request.Encode();
    }
    // 输出以下结果
    {"head":{"id":"123456","func":"alarming_request","time":""}}
    

    大家仔细的想象这里模板特化的好处,这个例子将模板特化用到了极致了,
    仔细品味这里的好处。
    JSON_ENCODE_FIELD(writer, "head", head);
    生成了这个"head":{"id":"123456","func":"alarming_request","time":""}
    如何做到的呢?
    首先JSON_ENCODE_FIELD(writer, "head", head);调用

    inline void JSON_ENCODE_FIELD(Writer<StringBuffer>&writer,const char* name,T &field)
    {
       writer.String(name); //"head"
       write_value(writer,field);
    }
    write_value(writer,field);调用下面
    inline void write_value(Writer<StringBuffer>&writer,T& field)
    {
       writer.StartObject();  //:{
       field.Encode(writer);
       writer.EndObject(); //}
    }
    field.Encode(writer); 调用如下
    void Encode(Writer<StringBuffer> &writer)
       {
           JSON_ENCODE_FIELD(writer, "id", id); // "id" :"123456"
           JSON_ENCODE_FIELD(writer, "func", func); //,"func":"alarming_request"
           JSON_ENCODE_FIELD(writer, "time", time); // ,"time":""
       };
    JSON_ENCODE_FIELD(writer, "id", id);
    inline void JSON_ENCODE_FIELD(Writer<StringBuffer>&writer,const char* name,T &field)
    {
       writer.String(name);  // "id"
       write_value(writer,field); // :"123456"
    }
    write_value(writer,field);  调用下面
    inline void write_value<std::string>(Writer<StringBuffer>&writer,std::string &field)
    {
       writer.String(field.c_str(),(SizeType)field.length()); // :"123456"
    }
    下面两个类似
    

    这里我选择都是string类型,所以只会调用
    inline void write_value<std::string>(Writer<StringBuffer>&writer,std::string &field) { writer.String(field.c_str(),(SizeType)field.length()); }
    如果换成不同的类型就会调用不同的特化函数了

    相关文章

      网友评论

          本文标题:用例子解释模板特化

          本文链接:https://www.haomeiwen.com/subject/ixbmsftx.html