d域问题

@safe:

struct S {
    int* ptr;
    
    auto borrowA() return {
        return ptr;
    }//模板
    
    int* borrowB() return /*域推导*/ {
        return ptr;
    }
}

void main() {
    static int* global;
    S s;
    global = s.borrowA;
    global = s.borrowB;
}

问题是,当是模板时,推导出了.

inout(D) borrowA()() return inout pure @safe nothrow @nogc {
    return _target; 
}
inout(D) borrowB() return inout pure @safe nothrow @nogc {//
    return _target; 
}

可加,

inout D temp = this._target;

来禁止推导.
thomas.bockman:
我不明白.return限制调用者使用返回值方式,scope限制被调使用this引用方式.
为什么A推导可删B地显式限制?
Dennis:
实现有问题,将用STC.returnScope替代.
依赖该问题
可用:

assert(!__traits(compiles, () @safe {...}));

测试是否允许转义引用.

posted @ 2021-11-28 17:34  zjh6  阅读(17)  评论(0)    收藏  举报  来源