d中各种域的区别

原文
return scope,scope returnreturn的区别?
为何构中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 returnscope(a)+ return ref(b)表明,(a),指针成员不会逃逸函数,(b)但可返回如&this.val构成员的引用.
局部变量上调用test(),则无论是否有,返回值都为域指针.
return表明允许返回如&this.val构成员引用,且因为无scope,可逃逸this.ptr等指针成员.但同样表明,不能在变量上调用test.
但,最新的2.100没有错.
无指针时,忽略了scope.
2.100前,ref参数上的return和scope不一致.

posted @ 2022-05-15 16:29  zjh6  阅读(71)  评论(0)    收藏  举报  来源