November 19, 2001
Often in C I want the compiler to generate a simple hash of a string or something at compile time... this is very troublesome as the compiler hardly ever does it all at compile time.  For instance:

Will the D compiler (with optimizations enabled) generate the same code for
this snippet:

{
  static const char[] str = "123";
  int checksum = 0;
  for (int i = 0; i < str.length; ++i)
    checksum += str[i];
}

as it would make for this snippet? :

{
  int checksum = '1' + '2' + '3';
}

It should, since everything involved in the first snippet is compile-time constants.

Sean


November 19, 2001
While the compiler could do such, it does not fold constants computed by loops. If you need them computed at compile time, what I'd do is write a program that generated the strings and their hashes, wrote out a module, and then compile that module.

"Sean L. Palmer" <spalmer@iname.com> wrote in message news:9tainn$1gb7$1@digitaldaemon.com...
> Often in C I want the compiler to generate a simple hash of a string or something at compile time... this is very troublesome as the compiler
hardly
> ever does it all at compile time.  For instance:
>
> Will the D compiler (with optimizations enabled) generate the same code
for
> this snippet:
>
> {
>   static const char[] str = "123";
>   int checksum = 0;
>   for (int i = 0; i < str.length; ++i)
>     checksum += str[i];
> }
>
> as it would make for this snippet? :
>
> {
>   int checksum = '1' + '2' + '3';
> }
>
> It should, since everything involved in the first snippet is compile-time constants.
>
> Sean
>
>