C ++ template metaprogramming (1)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.