成员的别名不会编译

时间:2014-06-16 20:12:13

标签: d

有人知道为什么这不起作用吗?

struct A
{
    int w;
    alias h = w; // works
}

struct B
{
    A a;
    alias w = a.w; // doesn't
}

void foo()
{
    A a;
    auto c = a.h;
    B b;
    b.w; // L18
}

dmd 2.065

aliastest.d(18): Error: struct aliastest.B 'w' is not a member
aliastest.d(18): Error: struct aliastest.B member w is not accessible
aliastest.d(18): Error: need 'this' for 'w' of type 'int'

1 个答案:

答案 0 :(得分:4)

因为它试图在静态上下文中访问a.w(与A.w相同),所以它不使用A结构的实例,而是使用A类型。这将有效:

struct A
{
    static int w;
    alias h = w; // works
}

struct B
{
    A a;
    // seems it is same as alias w = A.wl
    alias w = a.w; // does work too
}

void foo()
{
    B b;
    b.w = 8; // L18
}