0
点赞
收藏
分享

微信扫一扫

d中各种域的区别


​return scope​​,​​scope return​​和​​return​​的区别?

为何构中​​void* ptr​​改变了​​scope return​​的效果.

@safe:

struct A{
int val;
//void* ptr;

int* test() scope return{
return &this.val; // 好
}
}

struct B{
int val;
void* ptr;

int* test() return{
return &this.val; // 好
}
}

struct C{
int val;
void* ptr;

int* test() scope return{
return &this.val; // 错误,返回`&this.val`逃逸了`本`参引用
//最新2.100没有该错误.
}
}


void main(){}

​2.100​​更新了.

​return scope​​表明,指针成员,如​​this.ptr, C.ptr​​,除非返回​​它们​​,否则不会逃逸出​​函数​​.

如果在​​scope​​变量上调用​​test()​​,​​返回值​​会是个​​域针​​.

构成员上的​​scope return​​是​​scope(a)+ return ref(b)​​表明,(a),​​指针成员​​不会逃逸函数,(b)但可返回如​​&this.val​​构成员的​​引用​​.

在​​局部​​变量上调用​​test()​​,则无论是否有​​域​​,返回值都为​​域指针​​.

仅​​return​​表明允许返回如​​&this.val​​构成员​​引用​​,且因为无​​scope​​,可逃逸​​this.ptr​​等指针成员.但同样表明,不能在​​域​​变量上调用​​test​​.

但,最新的​​2.100​​没有错.

​构​​无指针时,忽略了​​scope​​.

在​​2.100​​前,​​ref​​参数上的​​return和scope​​不一致.



举报

相关推荐

0 条评论