现在的位置: 首页 > 综合 > 正文

C++模板元编程(1)

2013年09月17日 ⁄ 综合 ⁄ 共 3665字 ⁄ 字号 评论关闭

                     最近在看《C++ Template Metaprogramming》,学习模板也知道一些所谓的frist class 编程活动的原理。

                     元程序-Metaprogram,就是“a program about program”,就是操作代码的程序,我们最熟悉的也就是我们手头使用的编译器了,它将我们写的高级语言的源代码翻译成机器码。

                    数值计算是元编程应用的一个方面,它将运行期的计算提前到编译期,从而获得运行更快的程序。还有一个应用是在类型计算。

                    元函数和元数据是元编程的操作对象。

                    元函数的定义:

                    一个类模板,它的所有参数都是类型,或者是一个类,带有一个名为“type”的可公共访问的嵌套结构类型。

                   

                    元数据则复杂的多,两种最常见的是类型和整型,一些类型是为了达到一些效果而使用的外覆类和traits。

                     下面是我写的一个习题:   

                      对type进行伪英语描述

                       #ifndef TYPE_DES_H
#define TYPE_DES_H

#include <iostream>
#include <cstring>

const int MAX = 100;

template <typename T>
struct type_des
{
public:
    operator const char* (){
        return _name;
    }
    static const char* _name;
};

template <typename T>
const char* type_des<T>::_name = "undef type";

template <>
const char* type_des<int>::_name = "int";

template <>
const char* type_des<char>::_name = "char";

template <>
const char* type_des<long>::_name = "long";

template <>
const char* type_des<short>::_name = "short";

template <>
const char* type_des<void>::_name = "void";

template <>
const char* type_des<bool>::_name = "bool";

template <>
const char* type_des<float>::_name = "float";

template <>
const char* type_des<double>::_name = "double";

template <typename T>
struct type_des<T*>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"pointer to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename T>
struct type_des<T&>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"refence to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename T>
struct type_des<const T&>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"const refence to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};
template <typename T>
struct type_des<const T*>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"const pointer to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename T>
struct type_des<T[]>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"array of  ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename T>
struct type_des<T (*)()>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"function returning ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename T>
struct type_des<T* (*)()>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"function returning pointer to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename T>
struct type_des<T* (*[])()>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"array of function returning pointer to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename R,typename T>
struct type_des<T* (*[])(R)>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"array of function with ");
        strcat(temp,type_des<R>());
        strcat(temp," returning pointer to ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename R,typename T>
struct type_des<const T* (*[])(R)>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"array of function with ");
        strcat(temp,type_des<R>());
        strcat(temp," returning pointer to const ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

template <typename R,typename T>
struct type_des<const T& (*[])(R)>
{
    operator const char *(){
        static char temp[MAX];
        strcpy(temp,"array of function with ");
        strcat(temp,type_des<R>());
        strcat(temp," returning refence to const ");
        strcat(temp,type_des<T>());
        return temp;
    }
};

#endif // TYPE_DES_H

                   

                  

                     

抱歉!评论已关闭.