深入理解模板 模板元编程

来源:互联网 发布:台军反攻知乎 编辑:程序博客网 时间:2024/06/14 11:22
// Template.cpp : 定义控制台应用程序的入口点。
//

// 编译时编程
#include "stdafx.h"
#include <iostream>

template<int n>
struct Fib
{
    enum{ val = Fib<n-1>::val + Fib<n-2>::val };
};

template<>
struct Fib<1>
{
    enum{ val = 1 };
};

template<>
struct Fib<0>
{
    enum{ val = 0 };
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout<<Fib<5>::val<<std::endl;
    std::cout<<Fib<20>::val<<std::endl;

    system("pause");
    return 0;
}

// 编译时循环 1
#include "stdafx.h"
#include <iostream>

int power(int n, int p)
{
    int nReturnValue = (p == 0)? 1:(n*power(n, p-1));
    return nReturnValue;
}

template<int N, int P>
struct Power
{
    enum{ val = N * Power<N, P-1>::val };
};

template<int N>
struct Power<N,0>
{
    enum{ val = 1 };
};

int main()
{
    std::cout<<Power<2, 5>::val<<std::endl;

    system("pause");
    return 1;
}


// 编译时循环2
#include "stdafx.h"
#include <iostream>

using namespace std;

template<int n>
struct Identity
{
    enum{ val = n };
};

template<int n>
struct Square
{
    enum{ val = n * n };
};

template<int n>
struct Cube
{
    enum{ val = n * n * n };
};

template<int n, template<int> class F>
struct Accumulate
{
    enum{ val = Accumulate<n-1, F>::val + F<n>::val };
};

// 循环终止条件
template<template<int> class F>
struct Accumulate<0, F>
{
    enum{ val = F<0>::val };
};


int main()
{
    std::cout<<Accumulate<4, Identity>::val<<std::endl;
    std::cout<<Accumulate<4, Square>::val<<std::endl;
    std::cout<<Accumulate<4, Cube>::val<<std::endl;

    system("pause");
    return 0;
}


// 编译时选择
#include "stdafx.h"
#include <iostream>

using namespace std;

template<bool cond>
struct Select
{

};

template<>
class Select<true>
{
    static void statement1()
    {
        std::cout<<"This is Statement1 executing\n";
    }

public:
    static void f()  { statement1(); }
};


template<>
class Select<false>
{
    static void statement2()
    {
        std::cout<<"This is Statement2 executing\n";
    }

public:
    static void f() { statement2(); }
};

template<bool cond> void execute()
{
    Select<cond>::f();
}

int main()
{
    execute<true>();

    execute<false>();

    system("pause");
    return 0;
}


// 表达式模板
#include"stdafx.h"
#include<iostream>
#include<cstddef>
#include<cstdlib>
#include<ctime>
#include<iostream>

using namespace std;

template<class, size_t>
class MyVectorSum;

template<class T, size_t N>
class MyVector
{
    T data[N];

public:
    MyVector<T,N>& operator=(const MyVector<T,N>& right)
    {
        for(size_t i = 0; i < N; i++)
        {
            data[i] = right.data[i];
        }

        return *this;
    }

    MyVector<T,N>& operator=(const MyVectorSum<T,N>& right);

    const T& operator[] (size_t i) const
    {
        return data[i];
    }

    T& operator[] (size_t i)
    {
        return data[i];
    }
};


template<class T,size_t N>
class MyVectorSum
{
    const MyVector<T,N>& left;
    const MyVector<T,N>& right;

public:
    MyVectorSum(const MyVector<T,N>& lhs, const MyVector<T,N>& rhs)
        :left(lhs), right(rhs)
    {

    }

    T operator[] (size_t t) const
    {
        return left[t]+right[t];
    }
};

// MyVector 重载=运算符
template<class T, size_t N>
MyVector<T,N>& MyVector<T,N>::operator=(const MyVectorSum<T,N>& right)
{
    for(size_t i = 0; i < N; i++)
    {
        data[i] = right[i];
    }

    return *this;
}

// MyVectorSum 重载+运算符
template<class T, size_t N>
inline MyVectorSum<T,N> operator+(
    const MyVector<T,N>& left,
    const MyVector<T,N>& right
    )
{
    return MyVectorSum<T,N>(left, right);
}


// init 函数
template<class T, size_t N>
void init(MyVector<T,N>& v)
{
    for(size_t i = 0; i < N; i++)
    {
        v[i] = rand() % 100;
    }
}

// print 函数
template<class T, size_t N>
void print(MyVector<T,N>& v)
{
    for(size_t i = 0; i < N; i++)
    {
        cout<<v[i]<<"  ";
    }

    cout<<endl;
}

int main()
{
    unsigned int unSrandNum = (unsigned int)(time(0));
    srand(unSrandNum);

    MyVector<int, 5> v1;
    init(v1);
    print(v1);

    MyVector<int, 5> v2;
    init(v2);
    print(v2);

    MyVector<int, 5> v3;
    v3 = v1 + v2;
    print(v3);

    system("pause");
    return 0;
}



0 0
原创粉丝点击