Aliases

rm roel.mathys at gmail.com
Sun Oct 22 06:39:54 PDT 2006


Reiner Pope wrote:
> What's the purpose of the restriction of alias in the specs -- 'Aliases
> cannot be used for expressions'; it prevents some useful things like this:
> 
>   int[2] nodes;
>   alias nodes[0] left;
>   alias nodes[1] right;
> 
> which currently has to be written in a much more hackish way:
> 
>   union {
>     int[2] nodes;
>     struct {
>        int left;
>        int right;
>     }
>   }
> 
> This is less reliable because if you decide to change the datatype of
> nodes, you could easily stuff up:
> 
>   union {
>     long[2] nodes;
>     struct {
>        int left; // oops, forgot to change this. Now we've sliced the
> first value
>        int right;
>     }
>   }
> 

you could do this:

  template KT(T)
  {
    union KT
    {
      T[2] nodes;
      struct
      {
        T left;
        T right;
      }
    }
  }

  alias KT!(int) K;

roel



More information about the Digitalmars-d mailing list