小编sib*_*ick的帖子

opDispatch和编译时间参数

如何使用opDispatch转发到具有编译时参数的方法.见下面的代码:

import std.stdio;

struct B{
    auto p1(T)(T arg) {
        writeln( "p1: ", arg );
    }
    auto p2(T, int C)(T s) {
        writeln( "p2: ", s, " / ", C);
    }
}

struct C(T) {
    T b;

    auto opDispatch(string s, Args...)(Args args) {
        mixin("b."~s)(args);
    }
}

void main() {
     C!B b;
     //fine: compiler is smart enough
    b.p1("abc");
    //oops: "no property 'p2' for type ..."
    b.p2!(int, 10)(5);
    B origB;
    //fine:
    origB.p2!(int, 10)(5);
 }
Run Code Online (Sandbox Code Playgroud)

编辑

用struct替换class:避免使用CTFE进行字段初始化new.这与我的问题无关.

d

6
推荐指数
1
解决办法
312
查看次数

标签 统计

d ×1