c++ - 在 C++ 中绑定(bind)多个引用的临时对象的生命周期

标签 c++ standards temporary object-lifetime

C++ 标准草案 N4296 说

[class.temporary/5] The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except...

所以我想知道如果两个或多个引用绑定(bind)到一个临时文件会发生什么。它在标准中有具体规定吗?以下代码可能是一个示例:

#include <iostream> //std::cout
#include <string>   //std::string
const std::string &f() {
    const std::string &s = "hello";
    static const std::string &ss = s;
    return ss;
}
int main() {
    const std::string &rcs = f();
    std::cout << rcs; //empty output
                      //the lifetime of the temporary is the same as that of s
    return 0;
}

如果我们改变边界顺序,情况就不同了。

#include <iostream> //std::cout
#include <string>   //std::string
const std::string &f() {
    static const std::string &ss = "hello";
    const std::string &s = ss;
    return ss;
}
int main() {
    const std::string &rcs = f();
    std::cout << rcs; //output "hello"
                      //the lifetime of the temporary is the same as that of ss
    return 0;
}

编译在Ideone.com 完成。

我想 [class.temporary/5] 只有在 first 引用绑定(bind)到临时对象时才成立,但我在标准中找不到证据。

最佳答案

这是我报告为 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1299 的部分中的一个缺陷.

提议的决议是增加一个术语“临时表达式”。生命周期延长只发生在临时表达式引用的对象上。

这是我私下通过电子邮件发送的原始报告。我认为它清楚地说明了它是关于什么的

In the model of the Standard, there appears to be a distinction about temporary objects, and temporary expressions.

Temporary objects are created by certain operations operations, like functional casts to class types. Temporary objects have a limited specified lifetime.

Temporary expressions are expressions that are so attributed because they are used to track whether or not an expression refers to a temporary object for the purpose of determining whether or not the lifetime of their referent is lengthened when bound by a reference. Temporary expressions are compile time entities.

Several paragraphs refer to "temporaries", but do not explicitly specify whether they refer to temporary objects referred to by arbitrary expressions, or whether they refer only to temporary expressions. The paragraphs about RVO (paragraph 12.8p31) use "temporary" in the sense of temporary objects (they say such things like "temporary class object that has not been bound to a reference"). The paragraphs about lifetime lengthening (sub-clause 12.2) refer to both kinds of temporaries. For example, in the following, "*this" is not regarded as a temporary, even though it refers to a temporary

struct A { A() { } A &f() { return *this; } void g() { } };

// call of g() is valid: lifetime did not end prematurely
// at the return statement
int main () { A().f().g(); }

As another example, core issue 462 handles about temporary expressions (making a comma operator expression a temporary, if the left operand was one). This appears to be very similar to the notion of "lvalue bitfields". Lvalues that track along that they refer to bitfields at translation time, so that reads from them can act accordingly and that certain reference binding scenarios can emit diagnostics.

关于c++ - 在 C++ 中绑定(bind)多个引用的临时对象的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33846664/

相关文章:

C++混淆for循环中重新声明变量的范围

c++ - 从 C++ 生成和打印电子表格

c - 为什么 size_t when int 足以满足数组的大小?

java - PreparedStatement 只对数据库进行临时更改还是可以将其永久更改?

c++ - 聚合引用成员和临时生命周期

c++ - 仅在堆栈上构造的类;不带新的。 C++

c++ - 各种初始化和构造之间的关系?

java - 为什么在 Java 方法名中使用数字是错误的?

javascript - Chrome扩展程序保存临时文件