它的生存期如何扩展到调用函数的范围

它的生存期如何扩展到调用函数的范围

本文介绍了当将返回值绑定到调用函数中的const引用时,它的生存期如何扩展到调用函数的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从函数返回一个值(不是引用),然后将其绑定到调用函数中的const引用,则其寿命将扩展到调用函数的范围."

"If you return a value (not a reference) from the function, then bind it to a const reference in the calling function, its lifetime would be extended to the scope of the calling function."

所以:案例A

const BoundingBox Player::GetBoundingBox(void)
{
    return BoundingBox( &GetBoundingSphere() );
}

从函数GetBoundingBox()

变体I :(将其绑定到const引用)

variant I: (Bind it to a const reference)

const BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();

变体II :(将其绑定到const副本)

variant II: (Bind it to a const copy)

const BoundingBox l_Bbox = l_pPlayer->GetBoundingBox();

两者都能正常工作,而且我看不到l_Bbox对象超出范围. (不过,据我了解,在变体一中,未调用复制构造函数,因此它比变体II稍好.)

Both work fine and I don't see the l_Bbox object going out of scope. (Though, I understand in variant one, the copy constructor is not called and thus is slightly better than variant II).

为了比较,我进行了以下更改.

Also, for comparison, I made the following changes.

案例B

BoundingBox Player::GetBoundingBox(void)
{
    return BoundingBox( &GetBoundingSphere() );
}

带有变体:我

BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();

和II:

BoundingBox l_Bbox = l_pPlayer->GetBoundingBox();


对象l_Bbox仍然没有超出范围. 如何将其绑定到调用函数中的const引用,其生存期将扩展到调用函数的范围",如何将对象的生存期真正扩展到调用函数的范围?


The object l_Bbox still does not go out scope. How does "bind it to a const reference in the calling function, its lifetime would be extended to the scope of the calling function", really extend the lifetime of the object to the scope of the calling function ?

我在这里错过了一些琐碎的事情吗?

Am I missing something trivial here?

推荐答案

通常,临时对象(例如函数调用返回的对象)的生存期可以延长到封闭表达式"的末尾.但是,临时绑定到引用通常会将其生存期提升"为引用的生存期(可能是调用函数的生存期,也可能不是).但是有一些例外.在12.2/5临时对象"中的标准中对此进行了涵盖:

Normally a temporary object (such as one returned by a function call) has a lifetime that extends to the end of the "enclosing expression". However, a temporary bound to a reference generally has it's lifetime 'promoted' to the lifetime of the reference (which may or may not be the lifetime of the calling function), but there are a couple exceptions. This is covered by the standard in 12.2/5 "Temporary objects":

有关更多信息,请参见以下内容:

See the following for more information:

  • C++ constant reference lifetime (container adaptor)
  • GotW #88: A Candidate For the "Most Important const"

一个示例可能有助于可视化正在发生的事情:

An example that might help visualize what's going on:

#include <iostream>
#include <string>

class foo {
public:
    foo( std::string const& n) : name(n) {
        std::cout << "foo ctor - " << name + " created\n";
    };
    foo( foo const& other) : name( other.name + " copy") {
        std::cout << "foo copy ctor - " << name + " created\n";
    };

    ~foo() {
        std::cout << name + " destroyed\n";
    };

    std::string getname() const { return name; };
    foo getcopy() const { return foo( *this); };

private:
    std::string name;
};

std::ostream& operator<<( std::ostream& strm, foo const& f) {
    strm << f.getname();
    return strm;
}


int main()
{
    foo x( "x");

    std::cout << x.getcopy() << std::endl;

    std::cout << "note that the temp has already been destroyed\n\n\n";

    foo const& ref( x.getcopy());

    std::cout << ref << std::endl;

    std::cout << "the temp won't be deleted until after this...\n\n";
    std::cout << "note that the temp has *not* been destroyed yet...\n\n";
}

哪个显示:

foo ctor - x created
foo copy ctor - x copy created
x copy
x copy destroyed
note that the temp has already been destroyed


foo copy ctor - x copy created
x copy
the temp won't be deleted until after this...

note that the temp has *not* been destroyed yet...

x copy destroyed
x destroyed

这篇关于当将返回值绑定到调用函数中的const引用时,它的生存期如何扩展到调用函数的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:40