从函数返回对局部变量的 const 引用
我对从函数返回对局部变量的引用有一些疑问:
class A {
public:
A(int xx)
: x(xx)
{
printf("A::A()\n");
}
};
const A& getA1()
{
A a(5);
return a;
}
A& getA2()
{
A a(5);
return a;
}
A getA3()
{
A a(5);
return a;
}
int main()
{
const A& newA1 = getA1(); //1
A& newA2 = getA2(); //2
A& newA3 = getA3(); //3
}
我的问题是 =>
getA1()
的实现是否正确? 我觉得它不正确,因为它返回的是局部变量或临时变量的地址。main
(1,2,3) 中的哪些语句会导致未定义的行为?在
const A&中newA1 = getA1();
标准是否保证 const 引用的临时绑定在该引用超出范围之前不会被销毁?
I have some questions on returning a reference to a local variable from a function:
class A {
public:
A(int xx)
: x(xx)
{
printf("A::A()\n");
}
};
const A& getA1()
{
A a(5);
return a;
}
A& getA2()
{
A a(5);
return a;
}
A getA3()
{
A a(5);
return a;
}
int main()
{
const A& newA1 = getA1(); //1
A& newA2 = getA2(); //2
A& newA3 = getA3(); //3
}
My questions are =>
Is the implementation of
getA1()
correct?
I feel it is incorrect as it is returning the address of a local variable or temporary.Which of the statements in
main
(1,2,3) will lead to undefined behavior?In
const A& newA1 = getA1();
does the standard guarantee that a temporary bound by a const reference will not be destroyed until the reference goes out of scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
程序中唯一正确的
getAx()
版本是getA3()
。无论您以后如何使用它们,其他两个都有未定义的行为。从某种意义上说,他们都不是。对于 1 和 2,未定义的行为是函数体的结果。对于最后一行,
newA3
应该是一个编译错误,因为您无法将临时值绑定到非常量引用。不。下面是一个示例:
这里,
getA3()
返回一个临时变量,该临时变量的生命周期现在绑定到对象newConstA3
。换句话说,临时对象将一直存在,直到 newConstA3 超出范围。The only version of
getAx()
that is correct in your program isgetA3()
. Both of the others have undefined behaviour no matter how you use them later.In one sense none of them. For 1 and 2 the undefined behaviour is as a result of the bodies of the functions. For the last line,
newA3
should be a compile error as you cannot bind a temporary to a non const reference.No. The following is an example of that:
Here,
getA3()
returns a temporary and the lifetime of that temporary is now bound to the objectnewConstA3
. In other words the temporary will exist untilnewConstA3
goes out of scope.Q1:是的,这是一个问题,请参阅问题2的答案。
Q2:1和2是未定义的,因为它们引用了getA1和getA2堆栈上的局部变量。这些变量超出了范围,不再可用,更糟糕的是,随着堆栈不断变化,这些变量可能会被覆盖。 getA3 可以工作,因为创建了返回值的副本并将其返回给调用者。
问题 3:不存在这样的保证来看到问题 2 的答案。
Q1: Yes, this is a problem, see answer to Q2.
Q2: 1 and 2 are undefined as they refer to local variables on the stack of getA1 and getA2. Those variables go out of scope and are no longer available and worse can be overwritten as the stack is constantly changing. getA3 works since a copy of the return value is created and returned to the caller.
Q3: No such guarantee exists to see answer to Q2.
我认为主要问题是你根本没有返回临时变量,你应该
而不是
否则你返回的是局部变量地址,而不是临时变量。而临时到 const 的引用仅适用于临时变量。
我认为它的解释在这里:
临时为 const参考
I think the main problem is that you are not returning temporaries at all, you should
rather than
Otherwise you are returning local variable address, not temporary. And the temporary to const reference only works for temporaries.
I think its explained here:
temporary to const reference
如果您在 VC6 上编译它,您将收到此警告
******编译器警告(级别 1)C4172
返回局部变量或临时变量的地址
函数返回局部变量或临时对象的地址。当函数返回时,局部变量和临时对象会被销毁,因此返回的地址无效。 *****
在测试这个问题时,我发现了有趣的事情(给定的代码在 VC6 中工作):
If you will compile this on VC6 you will get this warning
******Compiler Warning (level 1) C4172
returning address of local variable or temporary
A function returns the address of a local variable or temporary object. Local variables and temporary objects are destroyed when a function returns, so the address returned is not valid.******
While testing for this problem i found interesting thing (given code is working in VC6):