C++ 警告:返回对临时的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1339601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: returning reference to temporary
提问by Hymanhab
I have a function like this
我有这样的功能
const string &SomeClass::Foo(int Value)
{
if (Value < 0 or Value > 10)
return "";
else
return SomeClass::StaticMember[i];
}
I get warning: returning reference to temporary
. Why is that? I thought the both values the function returns (reference to const char* "" and reference to a static member) cannot be temporary.
我明白了warning: returning reference to temporary
。这是为什么?我认为函数返回的两个值(对 const char* "" 的引用和对静态成员的引用)不能是临时的。
回答by Christian
This is an example when an unwanted implicit conversion takes place. ""
is not a std::string
, so the compiler tries to find a way to turn it into one. And by using the string( const char* str )
constructor it succeeds in that attempt.
Now a temporary instance of std::string
has been created that will be deleted at the end of the method call. Thus it's obviously not a good idea to reference an instance that won't exist anymore after the method call.
这是发生不需要的隐式转换的示例。""
不是 a std::string
,所以编译器试图找到一种方法将它变成一个。并且通过使用string( const char* str )
构造函数,它在该尝试中取得了成功。现在std::string
已经创建了一个临时实例,该实例将在方法调用结束时删除。因此,引用在方法调用后不再存在的实例显然不是一个好主意。
I'd suggest you either change the return type to const string
or store the ""
in a member or static variable of SomeClass
.
我建议您将返回类型更改为const string
或存储""
在SomeClass
.
回答by Emmanuel Caradec
This is the exemplary of trying to optimize code in c++. I did it, everybody did it... It is worth mentioning that this is the classical example that is eligible to return value optimization.
这是尝试优化 C++ 代码的示例。我做到了,大家都做到了…… 值得一提的是,这是一个符合返回值优化条件的经典例子。
Like ttvd said the correct answer is to return const std::string and not a reference to it and to let the compiler optimise it.
就像 ttvd 所说的那样,正确的答案是返回 const std::string 而不是对它的引用,并让编译器对其进行优化。
If you trust the interpreter of your favorite language to optimize behind you, you shouldn't try to be too smart with C++ either.
如果您相信您最喜欢的语言的解释器会在您身后进行优化,那么您也不应该尝试对 C++ 过于聪明。
回答by Shaggy Frog
The problem is in the first line. ""
will be turned into an std::string
, as it has a valid constructor that takes a char*
. That std::string
will be an anonymous object, which is temporary, and you return its reference.
问题出在第一行。""
将变成一个std::string
,因为它有一个有效的构造函数,它接受一个char*
. 那std::string
将是一个匿名对象,它是临时的,您返回它的引用。
回答by ttvd
Like Shaggy Frog said, it converts "" to a temporary std::string object and since your method signature is std::string& it attempts to return a reference to it, hence you get the warning. One workaround may be to return the std::string by value (const std::string SomeClass::Foo(..)).
就像 Shaggy Frog 所说的那样,它将 "" 转换为临时 std::string 对象,并且由于您的方法签名是 std::string& 它尝试返回对它的引用,因此您会收到警告。一种解决方法可能是按值返回 std::string (const std::string SomeClass::Foo(..))。
回答by Srinivasa Lakkaraju
The other possibility to avoid is to declare what you want to return as static and just use return by reference
避免的另一种可能性是将要返回的内容声明为静态并仅使用按引用返回