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