Recently I am reading "C ++ template metaprogramming". The learning template also knows the principles of some so-called frist class programming activities.
Metaprogram is the "A program about program", the program that operates the code, and the compiler we are most familiar, it translates the source code of the advanced language we wrote into machine code.
Numerical Computation is an aspect of metaprogramming applications. It advances runtime computation to the compilation phase to obtain programs that run faster. Another application is in type computing.
Meta functions and metadata are the operation objects of metaprogramming.
Definition of metacharacter functions:
A class template, all its parameters are types, or a class, with a public-accessible nested structure type named "type.
Metadata is much more complex. The two most common types are type and integer. Some types are used for overwriting and traits to achieve some results.
The following is an exercise I wrote:
Describe type in pseudo English
# 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 ");
Strcat (temp, type_des <t> ());
Return temp;
}
};
Template <typename T>
Struct type_des <t &>
{
Operator const char *(){
Static char temp [Max];
Strcpy (temp, "refence ");
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 ");
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 ");
Strcat (temp, type_des <t> ());
Return temp;
}
};
Template <typename T>
Struct type_des <t []>
{
Operator const char *(){
Static char temp [Max];
Strcpy (temp, "array ");
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 ");
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 ");
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 ");
Strcat (temp, type_des <r> ());
Strcat (temp, "returning pointer ");
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 ");
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 ");
Strcat (temp, type_des <r> ());
Strcat (temp, "returning refence to const ");
Strcat (temp, type_des <t> ());
Return temp;
}
};
# Endif // type_des_h