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 @   zjh6  阅读(66)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示