d的结构继承

import std.stdio;
import std.traits;

template JoinStruct(Ss...)
{
   static foreach (S; Ss)
   {
      static foreach(i, alias f; S.tupleof)
      {
         mixin("typeof(f) ", __traits(identifier, f), " = S.init.tupleof[i];");
      }
   }
}

void main(){

   struct A {
      int alpha;
      float x = 1.23;
   }

   struct B {
      int beta;
      float y = 4.4;
      string s = "很好";
   }

   struct C {
      int gamma = 42;
      double z = 1.2e8;
      string t = "如命名为s,则编译时检测重复";
   }

   struct D {
      mixin JoinStruct!(A,B,C);
   }//相当于(继承了)

   A a;
   B b;
   C c;

   writeln("\na:", a);
   writeln("\nb:", b);
   writeln("\nc:", c);
   auto d = D();
   writeln("\nd:", d);
}

posted @ 2021-09-17 14:16  zjh6  阅读(27)  评论(0)    收藏  举报  来源