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不一致.
浙公网安备 33010602011771号