“范围"的含义在D中(用于参数) [英] Meaning of "scope" in D (for a parameter)

查看:83
本文介绍了“范围"的含义在D中(用于参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作用域

void foo(scope void* p) { }

是平均值吗?

(我不是在谈论 scope(exit) scope int x = 5; ,而是关于在内部使用的 scope 参数列表.)

(I'm not talking about scope(exit) or scope int x = 5;, but about scope as used inside a parameter list.)

推荐答案

D中 scope 有3种用途.

  1. scope 语句.这是当您使用 scope(success) scope(failure) scope(exit)时.如果没有引发异常,引发异常或者是否为成功而引发异常,则退出 scope 语句所在的作用域时,将运行随后的块中的语句.,然后分别退出. scope 的这种用法仍停留在该语言中.

  1. scope statements. This is when you use scope(success), scope(failure), or scope(exit). The statements in the block that follows are run when exiting the scope that the scope statement is in if no exception is thrown, if an exception is thrown, or regardless of whether an exception is thrown for success, failure, and exit respectively. This use of scope is staying in the language.

作用域在局部变量上.这会将变量放在堆栈上-即使它是一个类.该对象离开作用域时将被销毁. scope 的这种使用是不安全的,最终会从该语言中删除(尽管 std.typecons.作用域替代了那些想要危险地生活的人.

scope on a local variable. This puts the variable on the stack - even if it's a class. The object is destroyed when it leaves scope. This use of scope is unsafe and will eventually be removed from the language (though std.typecons.scoped replaces it for those who want to live life dangerously).

scope (这是您要询问的用例).当放置在作为委托的参数上时,这意味着无法对该参数的引用进行转义(即,将其分配给全局变量).并且当编译器在委托上看到此错误时,它将避免在获取局部函数的地址时分配闭包.这在 opApply 循环(

scope on a function parameter (which is the use case that you're asking about). When placed on a parameter that is a delegate, it means that references to that parameter cannot be escaped (i.e. assigned to a global variable). And when the compiler sees this on delegates, it will avoid allocating a closure when taking the address of a local function. This is essential in opApply loops (reference post on newsgroup). Currently, scope has no effect on any function parameters other than delegates and is ignored for all other types, though it may or may not at some point in the future be expanded to affect types like pointers to prevent them from escaping the function.

在功能参数上使用时, in 关键字是 const范围的别名,这通常是功能参数上 scope 的获取方式误用了.

When used on a function parameter, the in keyword is an alias for const scope, which is frequently how scope on function parameters gets inadvertently used.

这篇关于“范围"的含义在D中(用于参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆