C++模板元編程(1)

來源:互聯網
上載者:User

                     最近在看《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

                   

                  

                     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.