4

それぞれが同じメンバーで始まるいくつかの構造体を定義したい:

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

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

これを達成するための mixin テンプレートは何ですか?

4

3 に答える 3

5
mixin template Common() {
   S s; // same member
}

struct A {
   mixin Common;
   X x;
}

struct B {
   mixin Common;
   Y y;
}

テンプレート ミックスインを参照してください

于 2013-06-26T14:02:36.633 に答える
3
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;
}

または (string-mixin を非表示にします):

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;
}
于 2013-06-26T22:54:32.207 に答える
0

デビッドの答えに対するわずかに異なる見方:

//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{} 構文はオプションです。代わりに通常の文字列を渡すことができます。

于 2013-06-27T19:37:19.133 に答える