Difficulty:
Preferring description: The idea involved in the text is not asking you to follow it, and this article is only equivalent to a "popular articles". The syntax of Template is more introduced here. For example, the conditions mentioned below are to be aware of the compile period constant.
C Template provides us with a compiled function. The compiler is actually equivalent to an interpreter when processing template. Take advantage of Template can increase the running speed of your code, you can reduce the complexity of code maintenance, and you can provide more scalability for the code. Let's try this way.
First, C Template replaces the IF / IF-ELSE statement
IF (condition)
Statement 1;
Else
Statement 2;
In C Template, we can write this way
Template
Struct my_if {};
Template <>
Struct my_if
Static void Go ()
{Statement 1;}
}
Template <>
Struct my_if
Static void Go ()
{Statement 2;
}
For example, we judge two integers
MY_IF <(1 <5)> :: gO ();
Maybe you will think that this IF / if-else's template is a little bigger, huh, huh, continue to look down.
Second, expand the C Template version of the IF / IF-ELSE to cope with Switch code
If you are an object-oriented master, you will definitely not place the code like Switch in your code, you will definitely use this task with abstraction, inheritance, polymorphism and interface. But now I have to change a thinking, implement it with C Template. In fact, the code here is different from the IF / IF-ELSe of the above, but when used for Switch, it can also reflect the OC principle.
INT I;
Switch (i)
{
Case value 1;
Statement 1;
Break;
Case value 2;
Statement 2;
Break;
DEFAULT:
Statement 3;
}
Here is the C Template version
Template
Struct my_switch {
Static void Go () {
Statement 3;
}
}
Template <>
Struct my_switch
Static void Go ()
{Statement 1;}
}
Template <>
Struct my_switch
Static void Go ()
{Statement 2;
}
The call is MY_SWITCH
You may still find the Switch benefits of the C Template version because it wants to write more code. However, you will think about it now, you will find that when you insert a new value for Switch, you have to return to Switch, and also modify the internal code. And use C Template but don't need you to care for my_switch where to define it. When you add a new value judgment, you only need you to add a code that judge the value in a suitable place.
Third, the numerical operation based on C Template
Calculate a step of constant N
Template
Struct Factorial {
Static const Int value = n * factorial
}
Template <>
Struct Factorial <1> {static const INT value = 1;
}
When this type of template instantiates Factorial
Std :: Cout << Factorial <10> << std :: endl;
Fourth, with the method of calculating the above value to produce more practical code
Whether it considers the prototype of the pointer, such as via INT *, get INT. We can write the following code
Template
Struct primitive_type {
Typedef t value_type;
}
Template
Struct primitive_type
Typedef t value_type;
}
Typedef int * Pint;
Primitive_type
Std :: cout << Obj << std :: endl;
It is now clear that OBJ is not int *, but an int type. However, there is a defect that T is int ** can't be int, but it is Int *, which is not what we want. Now only need to be simple to make a simple changes to the Primitive_Type's offset version, you can meet the requirements.
Template
Struct primitive_type
TypedEf TypeName Primitive_Type
}
Typedef int **** Pint;
Primitive_type
// the end