Is this a bug?

Li Jie cpunion at gmail.com
Tue Apr 11 21:25:42 PDT 2006


In article <e1hqph$q8g$1 at digitaldaemon.com>, David L. Davis says...
>Li Jie, I don't think it's a bug. Below is some code I pull together that I
>think is sort of what you were trying to do (just remove the '#' characters from
>the code, they're only there for holding the formating). But I could be
>wrong...it has happen before. ;)

Thanks David.

Your code is function call, my code is value-parameter template.

I want to write a "__uuidof" template, like VC++. A simple implemention is:

# template __uuidof(T:IUnknown)
# {
#     IID __uuidof = {0x00000000, 0x0000, 0x0000, [0xC0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x46]};
# }

It's too long.

I think the D-template can calculate it from a string, at compile-time. I write
some code:

# template HexStrToUbyte(char[] str)
# {
#     const ubyte HexStrToUbyte = cast(ubyte)HexStrToUlong!(str);
# }
# 
# template HexStrToUshort(char[] str)
# {
#     const ushort HexStrToUshort = cast(ushort)HexStrToUlong!(str);
# }
# 
# template HexStrToUint(char[] str)
# {
#     const uint HexStrToUint = cast(uint)HexStrToUlong!(str);
# }
# 
# template HexStrToUlong(char[] s)
# {
#     static if (s.length == 1)
#         const ulong HexStrToUlong = HexToUbyte_bug!((s[0]));
#     else
#         const ulong HexStrToUlong 
#             = HexToUbyte_bug!((s[length-1])) 
#               + 16 * HexStrToUlong!(s[0..length-1]);
# }
# 
# template HexToUbyte(char[] c)
# {
#     static if (c[0] >= '0' && c[0] <= '9')
#         const ubyte HexToUbyte = c[0] - '0';
#     else static if (c[0] == 'A' || c[0] == 'a')
#         const ubyte HexToUbyte = 0xa;
#     else static if (c[0] == 'B' || c[0] == 'b')
#         const ubyte HexToUbyte = 0xb;
#     else static if (c[0] == 'C' || c[0] == 'c')
#         const ubyte HexToUbyte = 0xc;
#     else static if (c[0] == 'D' || c[0] == 'd')
#         const ubyte HexToUbyte = 0xd;
#     else static if (c[0] == 'E' || c[0] == 'e')
#         const ubyte HexToUbyte = 0xe;
#     else static if (c[0] == 'F' || c[0] == 'f')
#         const ubyte HexToUbyte = 0xf;
# }
# 
# template HexToUbyte_bug(char c)
# {
#     static if (c >= '0' && c <= '9')
#         const ubyte HexToUbyte_bug = c - '0';
#     else static if (c == 'A' || c == 'a')
#         const ubyte HexToUbyte_bug = 0xa;
#     else static if (c == 'B' || c == 'b')
#         const ubyte HexToUbyte_bug = 0xb;
#     else static if (c == 'C' || c == 'c')
#         const ubyte HexToUbyte_bug = 0xc;
#     else static if (c == 'D' || c == 'd')
#         const ubyte HexToUbyte_bug = 0xd;
#     else static if (c == 'E' || c == 'e')
#         const ubyte HexToUbyte_bug = 0xe;
#     else static if (c == 'F' || c == 'f')
#         const ubyte HexToUbyte_bug = 0xf;
# }
# 
# template IIDFromStr(char[] str)
# {
#     const IID IIDFromStr = {
#         HexStrToUint!(str[0..8]),
#         HexStrToUshort!(str[9..13]), 
#         HexStrToUshort!(str[14..18]),
#         [
#           HexStrToUbyte!(str[19..21]),
#           HexStrToUbyte!(str[21..23]), 
#           HexStrToUbyte!(str[24..26]),
#           HexStrToUbyte!(str[26..28]), 
#           HexStrToUbyte!(str[28..30]),
#           HexStrToUbyte!(str[30..32]), 
#           HexStrToUbyte!(str[32..34]),
#           HexStrToUbyte!(str[34..36])
#         ]
#     };
# }
# 
# template __uuidof(T:IUnknown)
# {
#     IID __uuidof = IIDFromStr!("00000000-0000-0000-C000-000000000046");
# }
# 
# template __uuidof(T:IClassFactory)
# {
#     IID __uuidof = IIDFromStr!("00000001-0000-0000-C000-000000000046");
# }
# 
# 
# void main()
# {
#     IID iu  = __uuidof!(IUnknown);
#     IID icp = __uuidof!(IClassFactory);
# 
#     writefln(iu.Data1);
#     writefln(iu.Data2);
#     writefln(iu.Data3);
#     writefln(iu.Data4);
# }


IIDFromStr!("00000000-0000-0000-C000-000000000046");
and
{0x00000000, 0x0000, 0x0000, [0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x46]};

I like the first. 





More information about the Digitalmars-d mailing list