【问题标题】:Mixin template for defining structs with identical member用于定义具有相同成员的结构的 Mixin 模板
【发布时间】:2013-06-26 13:13:39
【问题描述】:

我想定义一些结构,每个结构都以相同的成员开始:

struct A {
    S s; // same member
    X x; // other members
}

struct B {
    S s; // same member
    Y y; // other members
}

什么是混合模板来实现这一点?

【问题讨论】:

    标签: templates mixins d template-mixins


    【解决方案1】:
    mixin template Common() {
       S s; // same member
    }
    
    struct A {
       mixin Common;
       X x;
    }
    
    struct B {
       mixin Common;
       Y y;
    }
    

    Template Mixin

    【讨论】:

    • 有没有更紧凑的方法? IE。包括模板中的结构语法
    • @CoreXii 但是为什么呢?该语法本身非常简洁,您几乎不会获得任何简洁性,但肯定会影响可读性......
    【解决方案2】:
    import std.array;
    
    struct S {}
    struct X {}
    struct Y {}
    
    mixin template Common() {
       S s; // same member
    }
    
    string struct_factory(string name, string[] args...) {
        return `struct ` ~ name ~ ` { 
                    mixin Common;
                ` ~ args.join("\n") ~ `
                }`;
    }
    
    mixin(struct_factory("A", "X x;"));
    mixin(struct_factory("B", "Y y;"));
    
    void main() {
        A a;
        B b;
    }
    

    或者(隐藏字符串混合):

    import std.array;
    
    struct S {}
    struct X {}
    struct Y {}
    
    private string struct_factory(string name, string[] args...) {
        return `struct ` ~ name ~ ` { 
                    mixin Common;
                ` ~ args.join("\n") ~ `
                }`;
    }
    
    mixin template Common() {
        S s;
    }
    
    mixin template Struct(string name, Args...) {
        mixin(struct_factory(name, [Args]));
    }
    
    mixin Struct!("A", "X x;");
    mixin Struct!("B", "Y y;");
    
    
    void main() {
        A a;
        B b;
    }
    

    【讨论】:

      【解决方案3】:

      对大卫的回答略有不同:

      //The actual building of the struct
      mixin template PrefilledImpl(string name, string common, string individual)
      {
          mixin("struct " ~ name ~ "{" ~ common ~ individual ~ "}");
      }
      
      //A sort of template currying
      mixin template Prefilled(string templateName, string common)
      {
          mixin("mixin template " ~ templateName ~ 
                "(string name, string individual) {mixin PrefilledImpl!(name, \""
                ~ common ~ "\", individual);}");
      }
      
      //declare the "prototype" for the struct
      mixin Prefilled!("StructWithInt", q{int a;});
      
      //declare the actual struct type
      mixin StructWithInt!("MyStructA", q{double b;});
      
      
      //and again for a different struct
      mixin Prefilled!("StructWithLots", q{float z; char[][] ns; ulong id;});
      mixin StructWithLots!("MyStructB", q{void* data;});
      
      void main()
      {
          MyStructA foo;
          foo.a = 2;
          foo.b = 4.65;
      
          MyStructB bar;
          bar.z = 3.2;
          //etc.....
      }
      

      仅供参考,q{} 语法是可选的,您可以只传递普通字符串。

      【讨论】:

        猜你喜欢
        • 2018-11-06
        • 2016-01-25
        • 2016-09-09
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多