Special template in vc6
As we all know, vc6 templates do not support special features.
In recent days, we have to try to implement some special examples in vc6.
First, calculate the maximum common divisor of M and N. The general functions are as follows:
Int gcd (int m, int N)
{
If (n = 0) return m;
Return gcd (n, m % N );
}
The code for bitwise template (calculate the maximum number of common contracts during the compilation period) is as follows:
Template <int M, int n>
Struct static_gcd
{
Enum {value = static <n, m % N >:: value };
}
Template <int m>
Struct static_gcd <m, 0>
{
Enum {value = m };
};
Because vc6 does not support bitrate conversion, this Code cannot be compiled. However, we can bypass this problem:
Template <int M, int n>
Struct static_gcd
{
Template <int unused>
Struct n_traits
{
Enum {value = static_gcd <n, m % N >:: value };
};
Template <>
Struct n_traits <0>
{
Enum {value = m };
};
Enum {value = n_traits <n >:: value };
};