C Coding Tips
Betta jin <
Betajin@hotmail.com>
[Draft] April {16, 19, 25}, 2002
Chapter3. Templates
Section1. Compile Time Routing
Compile Time IF-Else Structure:
// the deallocator template:
Template
Class deallocator;
// Specialize for if-else switch
Template <>
Class Deallocator
{
PUBLIC:
Template
Static Inline Void Free_PTR (T * Const & P)
{
Assert (p! = 0);
Delete [] P;
}
}
Template <>
Class Deallocator
{
PUBLIC:
Template
Static Inline Void Free_PTR (T * Const & P)
{
Assert (p! = 0);
Delete P;
}
}
// USE CASE:
Template
Class chello
{
...
T * m_p;
Inline void free ()
{
DEAALLOCATOR <__Array> :: free_ptr (m_p);
}
...
}
Note:. This sample demonstrates a way to wrap the delete / delete [] operation in a same function without an if-else routing Here goes the if-else structure, and more, there will be switch-case sturcture in the same way. I think the '_Array' Parameter Here Can Be Called As 'Value Traits' OR 'Switch Traits'.
Both VC6 and VC7 Cannot Compile The Following Code. But G 3 Does! So, for Compatible Considances, I do not suggest such code:
Template
Class CLS_Any;
Template
Class CLS_Any
{
...
}
Template
Class CLS_Any
{
...
}
Note:. This is something of partial specialization The code in my way (1) for partial specializtion is nice enough for such requirements and can be accepted by both VC6 / VC7 / g 3 There are some tricks to implement compound class partial. Specialization. I'll Describe it Later.vc6, VC7 and G 3 Cannot Compile Such Code At All:
Template
Void func_any (t * const & p);
Template
Void func_any
{
...
}
Template
Void func_any
{
...
}
Section2. Any-Type Routing
Description: There are libraries and they are likely to provide some same functions But the providers did not make any type of conference to specify the naming of functions / APIs This is a problem When we write a common lib, we are forced to... declare that the library is for or based on some library. It's just a burden that if some one wants the library that is based on one specified library to be used in another environment, he must write a piece of customed code to adapt the library. If The Coding Task Is Done with Little Ugly Code, It Is Just So Lucky. But Some Time, He Cannot Use The Library For Him To Use. So No Change A Adapter, He Has To Write His Own code instead to use the library This is not only a compatible problem, but the library's self-adaptability I'll try to give a solution for such requirement by examples Any-type string:... use case of Any-type string: