临时对象有作用域吗?

发布于 2024-10-04 08:50:02 字数 211 浏览 3 评论 0原文

名称具有范围(编译时属性),而对象具有生命周期(运行时属性)。正确的?

我经常看到人们谈论临时对象“超出范围”。但由于临时对象没有名称,我认为在这种情况下谈论“范围”是没有意义的。临时对象的生命周期非常明确地定义并且与范围无关。你同意吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝海似她心 2024-10-11 08:50:02

名称具有范围(编译时属性),

是的。我不会称其为财产思想。但基本上是的。

而对象有生命周期(运行时属性)。对吗?

变量分为三种类型。每种类型在生命周期方面都有不同的属性。

  • 自动存储持续时间:
  • 静态存储持续时间
  • 动态存储持续时间

注意:自动存储持续时间对象的生命周期与变量的范围绑定。

我经常看到人们谈论临时对象“超出范围”。

除非绑定到变量,否则临时变量将在表达式末尾被销毁。如果它们绑定到变量(常量引用),那么它们具有与变量相同的生命周期。有时将其称为范围更容易,但从技术上讲,您是正确的。

但是由于临时对象没有名称,我认为在这种情况下谈论“范围”是没有意义的。

从技术上来说是的。但我认为这只是让谈论它变得更容易。对我来说(尽管技术上不正确)临时(未绑定)的范围是表达式。比临时变量的生命周期说起来容易一些。

临时对象的生命周期定义非常明确,与范围无关。你同意吗?

是的。但谈论范围仍然感觉更自然(即使它在技术上不正确)。大多数人都明白你想暗示什么。但是,当您坐下来谈论非常技术性的内容时,您应该使用正确的术语,并且在这种情况下范围是不正确的。

Names have scope (a compile-time property),

Yes. I would not call it a property thought. But basically yes.

while objects have lifetimes (a runtime property). Right?

There are three types of variables. Each type has different properties in relation to lifetimes.

  • Automatic storage duration:
  • Static storage duration
  • Dynamic storage duration

Note: automatic storage duration objects have a lifetime that is bound to the scope of the variable.

I often see people talking about temporary objects "going out of scope".

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.

But since a temporary object does not have a name, I think it does not make sense to talk about "scope" in this context.

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.

The lifetime of a temporary object is very clearly defined and has nothing to do with scope. Would you agree?

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.

静谧 2024-10-11 08:50:02

临时变量的生命周期与语法块关系不大,但“范围”——作为一个词而不是一个技术术语——可以以其他方式使用。重要的问题是,当人们使用“范围”来指代临时对象时,您是否感到困惑。 (从我的观点来看,你似乎不是。)

既然你正在谈论使用这个术语与他人交流,那么交流才是真正重要的。如果您通过编写标准文档来定义术语或尝试在已定义术语的上下文中解释此类文档,情况会有所不同。当然,解释 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.

巨坚强 2024-10-11 08:50:02

我见过有人说“对象超出范围”,而这意味着(用您的话说)“当对象的名称超出范围时,对象的生命周期就结束了”。如果您使用这种简短形式,很自然地说临时对象也超出了范围。

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.

百变从容 2024-10-11 08:50:02

临时对象确实有名称,尽管只能由编译器引用。否则编译器将如何引用它们?仅仅因为实例化后就无法引用临时对象,并不意味着编译器无法引用它。

f(Foo(), Bar());

编译器必须至少引用其中一个临时变量,即使您作为程序员不能引用其中任何一个。临时对象确实有一个范围。

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.

f(Foo(), Bar());

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.

很糊涂小朋友 2024-10-11 08:50:02

绑定到 const 引用将临时引用的生命周期延长到引用的生命周期,因此从某种意义上说,它确实在这种特殊情况下与作用域有关:

std::string foo();

int main()
{
    // Lifetime of the temporary returned by foo is indeed the scope of bar
    const std::string &bar = foo();
}

请参阅这篇文章来自赫伯·萨特:

通常,临时对象会持续存在
只到完整结束为止
它出现的表达式。
然而,C++故意指定
将临时对象绑定到
对堆栈上 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 :

std::string foo();

int main()
{
    // Lifetime of the temporary returned by foo is indeed the scope of bar
    const std::string &bar = foo();
}

See this article from Herb Sutter :

Normally, a temporary object lasts
only until the end of the full
expression in which it appears.
However, C++ deliberately specifies
that binding a temporary object to a
reference to const on the stack
lengthens the lifetime of the
temporary to the lifetime of the
reference itself, and thus avoids what
would otherwise be a common
dangling-reference error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文