#include <iostream>
#include <string>
using namespace std;
const string& Func()
{
return "123";
}
int main()
{
string s = Func();
cout << s << endl;
return 0;
}
const 引用不是会提升临时变量的生命期吗? 为什么返回函数内的临时变量会报错了? 我对const引用的理解哪里错了吗?
#include <iostream>
#include <string>
using namespace std;
const string& Func(const string& s)
{
return s;
}
int main()
{
string s = Func("123");
cout << s << endl;
return 0;
}
但是这段代码就可以了。 Func函数参数绑定到一个临时变量,然后返回这个临时变量的const引用,就没有问题?
why?
Because of the former, "123" is inside the Func function, that is, on the stack of the function. It is gone when the function is exited.
The latter "123" means that the Func function inside the main function has been executed, and the control flow returns to the main function.
The const reference will increase the life cycle of the temporary variable. It means that the life cycle of the temporary variable is originally just the expression that creates the temporary variable. After the expression ends, it is destructed. The const reference will increase its life cycle until the end of the function. (if it is a global const reference variable, it will naturally be promoted to the life cycle of the entire program), the function is destructed at the end, and its life cycle will not be promoted outside the function. When the function ends, it will also be destructed. of.
Const references are actually similar to rvalue references. They both promote the life cycle of temporary variables from the expression to the function.
Add some reference materials
Quoted from http://en.cppreference.com/w/cpp/language/reference_initialization
Translate the first two paragraphs, paying special attention to the bold words:
Whenever a reference is bound to a temporary variable or a base class subobject of a temporary variable, the life cycle of the temporary variable is extended to Corresponds to the lifetime of the reference , with the following exceptions:
A temporary binding to the return value of the function in the return statement of the function will not be Extended : It will be destroyed immediately at the end of the return statement. Such functions always return a dangling reference.
The main method of the first program:
is changed to:
const string s = Func();
Returning local variables defined within a function is undefined behavior.
Const here will not increase the life cycle of local variables. Local variables are stored on the stack. When the function returns, the corresponding stack will be released, that is, the local variables will also be destroyed. Your first program is not a problem with the local variable declaration cycle. Const means it cannot be modified. However, your s variable is not modified with const, so it cannot be assigned a value.
Personally, I think there are some problems with the above answers. . .
Look at the question’s code:
This code will indeed report an error, but just remove the
const string& Func()
in&
. The reason for the error is not that "123" is on the stack and is popped out of the stack when the function returns. Instead, "123" itself is in the static data area. When the function returns, what is actually returned is the address of "123" in the static data area. This address is a local variable (in the stack) in the function Func. At this time, We use the reference type to return the local variable (however, a pop operation will occur), and our reference variable will be released, and an error will occur. However, if we do not use&
, what we actually get when returning is the intermediate variable of the address value (the non-reference return value of the function will be stored in the intermediate variable). At this time, the stack operation does not affect us to obtain the correct address. Worth it.