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
不一致.