临时对象有作用域吗?
名称具有范围(编译时属性),而对象具有生命周期(运行时属性)。正确的?
我经常看到人们谈论临时对象“超出范围”。但由于临时对象没有名称,我认为在这种情况下谈论“范围”是没有意义的。临时对象的生命周期非常明确地定义并且与范围无关。你同意吗?
Names have scope (a compile-time property), while objects have lifetimes (a runtime property). Right?
I often see people talking about temporary objects "going out of scope". But since a temporary object does not have a name, I think it does not make sense to talk about "scope" in this context. The lifetime of a temporary object is very clearly defined and has nothing to do with scope. Would you agree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。我不会称其为财产思想。但基本上是的。
变量分为三种类型。每种类型在生命周期方面都有不同的属性。
注意:自动存储持续时间对象的生命周期与变量的范围绑定。
除非绑定到变量,否则临时变量将在表达式末尾被销毁。如果它们绑定到变量(常量引用),那么它们具有与变量相同的生命周期。有时将其称为范围更容易,但从技术上讲,您是正确的。
从技术上来说是的。但我认为这只是让谈论它变得更容易。对我来说(尽管技术上不正确)临时(未绑定)的范围是表达式。比临时变量的生命周期说起来容易一些。
是的。但谈论范围仍然感觉更自然(即使它在技术上不正确)。大多数人都明白你想暗示什么。但是,当您坐下来谈论非常技术性的内容时,您应该使用正确的术语,并且在这种情况下范围是不正确的。
Yes. I would not call it a property thought. But basically yes.
There are three types of variables. Each type has different properties in relation to lifetimes.
Note: automatic storage duration objects have a lifetime that is bound to the scope of the variable.
Unless bound to a variable a temporary is destroyed at the end of an expression. If they are bound to a variable (a const reference) then they have the same lifespan as the variable. Sometimes it is just easier to refer to this as the scope, but technically you are correct.
Technically yes. But I think it just makes talking about it easier. To me (though not technically correct) the scope of a temporary (not bound) is the expression. Its easier to say than the lifespan of the temporary variable.
Yes. But it still feels more natural to talk about scope (even if it is not technically correct). As most people understand what you are trying to imply. But when you get down and talk about the very technical stuff you should use the correct terminology and scope in this context is not correct.
临时变量的生命周期与语法块关系不大,但“范围”——作为一个词而不是一个技术术语——可以以其他方式使用。重要的问题是,当人们使用“范围”来指代临时对象时,您是否感到困惑。 (从我的观点来看,你似乎不是。)
既然你正在谈论使用这个术语与他人交流,那么交流才是真正重要的。如果您通过编写标准文档来定义术语或尝试在已定义术语的上下文中解释此类文档,情况会有所不同。当然,解释 ISO 14882 需要与他人沟通,因此在这种情况下,您只需在必要时要求澄清即可。
让所有非标准化通信标准化会适得其反,并且当需要高精度时,在任何一种情况下使用代码通常会更好。由于这个原因,C++ 标准广泛使用了示例。
再比如,经常使用“调用构造函数”,但从技术上讲,你不能直接调用构造函数;相反,构造函数是对象初始化的一部分。这就是为什么有一个显式的 new 形式专门用于构造一个对象。 (有趣的是,您可以直接调用析构函数。)但是,我希望在大多数上下文中都能理解该短语,尽管我不提倡在标准上下文中使用它。
The lifetime of temporaries has very little to do with syntactical blocks, but "scope" — as a word rather than a technical term — can be used in other ways. The important question is whether you are confused when people use "scope" to refer to temporaries. (It doesn't appear that you are, from my POV.)
Since you're talking about using the term to communicate with others, that communication is what's really important. If you were defining terms by writing a standardese document or trying to interpret such a document in the context of defined terms, the situation would be different. Interpreting ISO 14882 will, of course, involve communicating with others, so you would just have to ask for clarification if necessary, in that case.
It's counter-productive to make all non-standardese communication be standardese, and it's often better to use code in either case when high precision is required. The C++ standard extensively uses examples for this reason.
For another example, "calling a constructor" is often used, yet technically you can't call a ctor directly; instead, ctors are part of object initialization. This is why there's an explicit form of new solely to construct an object. (Interestingly, you can call a destructor directly.) However, I would expect that phrase to be understood in most contexts, though I wouldn't advocate using it in standardese contexts.
我见过有人说“对象超出范围”,而这意味着(用您的话说)“当对象的名称超出范围时,对象的生命周期就结束了”。如果您使用这种简短形式,很自然地说临时对象也超出了范围。
I've seen people say that "an object went out of scope" when it meant (in your parlance) "the lifetime of the object ended when the object's name went out of scope". If you use that short form, it's natural to say that temporay objects go out of scope, too.
临时对象确实有名称,尽管只能由编译器引用。否则编译器将如何引用它们?仅仅因为实例化后就无法引用临时对象,并不意味着编译器无法引用它。
编译器必须至少引用其中一个临时变量,即使您作为程序员不能引用其中任何一个。临时对象确实有一个范围。
Temporary objects do have names, albeit referable by the compiler only. Otherwise how would the compiler refer to them? Just because you can't refer to a temporary once it's instantiated doesn't mean the compiler can't refer to it.
The compiler has to refer to at least one of the temporaries even though you as a programmer can't refer to either of them. Temporary objects do have a scope.
绑定到 const 引用将临时引用的生命周期延长到引用的生命周期,因此从某种意义上说,它确实在这种特殊情况下与作用域有关:
请参阅这篇文章来自赫伯·萨特:
Binding to a const reference extends the lifetime of a temporary to the lifetime of the reference, so in a sense, it does have something to do with scope in this particular case :
See this article from Herb Sutter :