[C++ 2011 STL (VS2012 Update4) 源代码阅读系列(1)]准备知识


/*
prepare.cpp
演示了阅读 STL 原代码的一些预备知识
1. 模版以及模版的偏特化,是 STL 源代码里面用的很多的技术,
有效的解决了支持不同参数个和类型的问题;
2. 各式各样的宏定义,用于定义构造函数,重载函数实现,需要不断的展开,
这个也是平时代码很少见的特征
*/

//标明是使用 MS版本,本处使用 VS2012 update 4
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <functional>

using namespace std;
 //for _1, _2, _3...
//using namespace std::placeholders;

#define AA 88
#define join_1(a,b) a+b
//连接在一起,无法转义
#define join_2(a,b) ab
#define join_3(a,b) a##b
#define join_4(a,b) v_ ## a ## _ ## b ## _t
#define join_5(a,b,XX) a<<","<<b<<","<<xy<<","<<XX

void test_micro()
{
    //演示了宏的一些定义
    int x = 11,y = 22;
    std::cout<<join_1(x,y)<<endl;
    //注意,是输出了变量的值
    int ab = 333;
    std::cout<<join_2(x,"y")<<endl;
    int xy = 999;
    std::cout<<join_3(x,y)<<endl;
    int v_11_22_t = 9999;
    std::cout<<join_4(11,22)<<endl;
    int v_x_y_t = 99999;
    std::cout<<join_4(x,y)<<endl;
    std::cout<<join_5(x,y,AA)<<std::endl;
    //下面一行编译失败,缺少参数
    //std::cout<<join_5(x,y)<<std::endl;
}


//声明一个模版类_Bind_A,容许至少有一个模板参数
//_Bind_A 实例化的时候最多只能有4个模板实参
//最后一个class = Null_class,无参数名称,是标明是不命名模板参数,
//Null_class 是一个空结构体,定义是: struct Null_class{};
//STL 原代码里面定义了一个 _Nil
struct Null_class{};
template<class _Fun,class v0_t = Null_class,class v1_t = Null_class,class = Null_class> class _Bind_A;

//专门化(偏特化)模版类
template<class _Fun> class _Bind_A<_Fun>
{
private:
    _Fun m_v;
public:
    _Bind_A(_Fun v):m_v(v)
    {
        //
    }

    const std::string cat()
    {
        std::stringstream str;
        str<<m_v<<endl;
        return str.str();
    }
};

template<class _Fun,class v0_t> class _Bind_A<_Fun,v0_t>
{
private:
    _Fun m_v;
public:
    _Bind_A(_Fun v):m_v(v)
    {
        //
    }

    const std::string cat(v0_t &v)
    {
        std::stringstream str;
        str<<m_v<<v<<endl;
        return str.str();
    }
};

//专门化(偏特化)模版类,同时演示了模板继存的方式
template<class _Fun,class v0_t,class v1_t> class _Bind_A<_Fun,v0_t,v1_t>
: public std::binary_function<v0_t,v1_t,bool>
{
private:
    _Fun m_v;
public:
    _Bind_A(_Fun v):m_v(v)
    {
        //
    }

    const std::string cat(v0_t &v,v1_t vv)
    {
        std::stringstream str;
        str<<m_v<<v<<vv<<endl;
        return str.str();
    }
};

//专门化(偏特化)模版类,同时演示了模板继存和多模版参数应用
template<class _Fun,class v0_t,class v1_t,class v2_t,class v3_t,class v4_t>
class _Bind_A<_Fun,v0_t,v1_t (),v2_t (v3_t,v4_t)> 
    : public std::binary_function<v1_t,v2_t,bool>
{
private:
    _Fun m_v;
public:
    _Bind_A(_Fun v):m_v(v)
    {
        //
    }

    const std::string cat(v0_t &v,v1_t vv)
    {
        std::stringstream str;
        str<<m_v<<v<<vv<<endl;
        return str.str();
    }
};

void test_template()
{
    _Bind_A<int> b_1(10);
    std::cout<<b_1.cat();

    _Bind_A<char,std::string> b_2('C');
    std::string s = "aa";
    std::cout<<b_2.cat(s);

    _Bind_A<char,std::string,double> b_3('c');  
    std::cout<<b_3.cat(s,99.99); 
 
    _Bind_A<std::string,int,int(),int(int,int)> b_4("DD");
    int i = 100;
    std::cout<<b_4.cat(i,9);
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout<<"test_micro start\n";
    test_micro();
    std::cout<<"test_micro end\n";
   
    std::cout<<"\ntest_template start\n";
    test_template();
    std::cout<<"test_template end\n";

    system("pause");

    return 0;
}

/*
//输出结果:

test_micro start
33
333
999
9999
99999
11,22,999,88
test_micro end

test_template start
10
Caa
caa99.99
DD1009
test_template end
请按任意键继续. . .
*/

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值