(Manifest) constants inside structs

Sönke Ludwig ludwig at informatik_dot_uni-luebeck.de
Tue Jan 22 15:12:05 PST 2008


I'm currently trying to upgrade from DMD 2.008 to 2.010. There's one problem I 
could not find a work-around. There are a bunch of structs which have constants 
defined inside:

   struct S {
     static const S some_const1 = {0};
     static const S some_const2 = {1};

     int member;
   }

This works fine, until there is a second struct, which uses on of these 
constants in an equivalent initializer:

   struct T {
     static const T dependent_constant = {S.some_const1};
     // <- error: cannot convert const(S) to S

     S member;
   }

I've tried to use an enum instead, but that causes a segfault in the compiler:

   struct S {
     enum S some_const1 = {0}; // compiler segfault without error message
   }

A possible solution would be to put the constants outside of the struct. 
However, the actual structs look more like:

   template(S, int N) Vector {
     static const S zero = {...};
     static const S one = {...};
   }
   alias Vector!(int, 2) Vec2i;

   // in code used as Vec2i.one

Since the types used for S are not known, putting the constants outside of the 
struct is not possible.

I think the semantics should be changed to allow assignment of const values to 
non-const variables, as long as the specific struct does not contain a reference 
  /pointer of some form (directly or indirectly).

Also manifest constants inside structs using enum would be nice.

Does anybody have another idea how to sidestep this issue for now?
I think I'm running out of ideas after also having tried a bunch of other things.



More information about the Digitalmars-d mailing list