Foreword
The C template is an extension of C , and the template is injected into new vitality into C . C template is divided into function templates and class templates. The C template is not unfamiliar with most C programmers. The C template has certain customization characteristics.
Customization of C Template: Custom Function Template
The basic syntax of the function template is as follows:
Template
T fun (t t1, ...)
{
Return T1;
}
T can be any type, but what should you do when you want to do a different type of handle? Such as:
Template
T Max (T T1, T T2, T T3)
{
T Temp = T1;
IF (TEMP IF (Temp Return Temp; } This function can handle three identical types of elements and remove their maximum. Under normal circumstances, this is right, but when there is a call to occur, it is not necessarily. Char array1 [] = "WinMain"; Char array2 [] = "yuankai"; Char array3 [] = "main"; Char * cp = max (array1, array2, array3); Since T is incorporated, the MAX function is only compared to the size of the pointer (address), which may not be the result we want. To resolve this issue, we must reserve the original function processing capability. We can use custom function templates, as follows: Char * max (Char * S1, Char * S2, Char * S3) { CHAR * CP = S1; IF (strCMP (CP, S2) <0) CP = S2; IF (strCMP (CP, S3) <0) CP = S3; Return CP; } We completed a customization of a MAX function and custom type char *. In this way, we have the following calls to get the correct result. Char * cp = max (array1, array2, array3); Not? Our output is what we want, through custom function templates, which can complete specific features and use its original features, is this not very good? Understand the custom function template makes us have a bigger spirit when using the function template Activity, give us more free space. The rush of the article, there is a typison or error, please pay more. Welcome to communicate with you. If everyone can, I will continue to write out (----- Yuan Kai -----).