未定义的行为和临时 [英] Undefined behavior and temporaries

查看:195
本文介绍了未定义的行为和临时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)它是未定义的行为返回一个临时的引用,即使该引用不使用?例如,这个程序保证输出好:

1) Is it undefined behavior to return a reference to a temporary, even if that reference is not used? For example, is this program guaranteed to output "good":

int& func()
{
    int i = 5;
    return i;
}

int main()
{
    func();

    cout << "good" << endl;
    return 0;
}

2)它是未定义的行为,简单地引用一个对象,更长的存在,即使该引用不使用?例如,这个程序保证输出好:

2) Is it undefined behavior to simply have a reference to an object that no longer exists, even if that reference is not used? For example, is this program guaranteed to output "good":

int main()
{
    int *j = new int();
    int &k = *j;
    delete j;

    cout << "good" << endl;
    return 0;
}

3)结合这些行为是未定义的行为吗?

3) Is it undefined behavior to combine these?

int& func()
{
    int i = 5;
    return i;
}

int main()
{
    int& p = func();

    cout << "good" << endl;
    return 0;
}


推荐答案

规则禁止case 1和2也不能找到一个相关的缺陷报告。

I don't see any rules that forbid case 1 and 2 nor can I find a relevant defect report either.

我们从草案C ++标准的所有我们从 8.3.2 [dcl.ref]

All we really have from the draft C++ standard is from section 8.3.2 [dcl.ref]:


<引用应初始化为引用有效对象或
函数。 [注意:特别是,一个空引用不能存在于一个定义良好的程序中,因为
创建这样的引用的唯一方法是将它绑定到通过空指针间接获得的对象$ [...]

[...]A reference shall be initialized to refer to a valid object or function. [ Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the "object" obtained by indirection through a null pointer, which causes undefined behavior.[...]

这不适用于case 1,因为我们不初始化一个引用并且既没有情况2,因为当我们初始化引用时,对象是有效的

which does not apply to case 1 since we are not initialing a reference and neither case 2 since the object is valid when we initialize the reference.

这似乎适用于case 3.那么有效对象是什么意思是以下缺陷报告的主题。涵盖这个主题的缺陷报告仍然是开放的,因此我们只能得到当前思维的感觉,这是应该是未定义的行为。

This does seem to apply to case 3. So what does valid object mean is subject of the following defect report. The defect report which covers this topic is still open and therefore we can only get a a feel for current thinking which is that this should be undefined behavior.

如果我们看看< a href =http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#453 =nofollow>缺陷报告453:引用只能绑定到有效对象,它处理绑定对无效对象的引用的含义。当前提议的解决方案说:

If we look at defect report 453: References may only bind to "valid" objects , which deals with what it means to bind a reference to an invalid object. The current proposed resolution says:


[...]如果引用直接绑定的左值既不指定现有对象或函数的合适类型(8.5.3 [dcl.init.ref]),也不是适当大小和对齐的存储区域,以包含引用类型的对象(1.8 [intro.object],3.8 [basic.life] 3.9 [basic.types]),行为是未定义的。 [...]

[...]If an lvalue to which a reference is directly bound designates neither an existing object or function of an appropriate type (8.5.3 [dcl.init.ref]), nor a region of storage of suitable size and alignment to contain an object of the reference's type (1.8 [intro.object], 3.8 [basic.life], 3.9 [basic.types]), the behavior is undefined. [...]

所以我们可以说目前的想法是这应该是未定义的行为,但目前这是缺陷,所以我们不能肯定地说,直到该缺陷报告被解决。我会错误地谨慎,并假设它是未定义的行为。

So we can say the current thinking is that this should be undefined behavior but currently this is defect and so we can't say for sure until this defect report is resolved. I would err on the side of caution and assume it is undefined behavior.

这篇关于未定义的行为和临时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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