警告:返回对临时的引用
我有一个像这样的函数,
const string &SomeClass::Foo(int Value)
{
if (Value < 0 or Value > 10)
return "";
else
return SomeClass::StaticMember[i];
}
我收到警告:返回对临时的引用
。这是为什么?我认为函数返回的两个值(对 const char* "" 的引用和对静态成员的引用)不能是临时的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是发生不需要的隐式转换的示例。
""
不是std::string
,因此编译器尝试找到一种方法将其转换为 std::string。通过使用 string( const char* str ) 构造函数,该尝试成功了。现在已经创建了一个 std::string 的临时实例,该实例将在方法调用结束时被删除。因此,引用方法调用后不再存在的实例显然不是一个好主意。
我建议您将返回类型更改为 const string 或将
""
存储在SomeClass
的成员或静态变量中。This is an example when an unwanted implicit conversion takes place.
""
is not astd::string
, so the compiler tries to find a way to turn it into one. And by using thestring( 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.I'd suggest you either change the return type to
const string
or store the""
in a member or static variable ofSomeClass
.这是尝试优化 C++ 代码的典范。我做到了,大家都做到了...
值得一提的是,这是符合返回值优化条件的经典例子。
就像 ttvd 所说,正确的答案是返回 const std::string 而不是对它的引用,并让编译器优化它。
如果您相信您最喜欢的语言的解释器会在您身后进行优化,那么您也不应该尝试对 C++ 过于聪明。
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.
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.
If you trust the interpreter of your favorite language to optimize behind you, you shouldn't try to be too smart with C++ either.
问题出在第一行。
""
将转换为std::string
,因为它有一个采用char*
的有效构造函数。该 std::string 将是一个匿名对象,它是临时的,并且您返回它的引用。The problem is in the first line.
""
will be turned into anstd::string
, as it has a valid constructor that takes achar*
. Thatstd::string
will be an anonymous object, which is temporary, and you return its reference.就像 Shaggy Frog 所说,它将 "" 转换为临时 std::string 对象,并且由于您的方法签名是 std::string&它尝试返回对其的引用,因此您会收到警告。一种解决方法可能是按值返回 std::string (const std::string SomeClass::Foo(..))。
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(..)).
另一种要避免的可能性是将您想要返回的内容声明为静态并仅使用引用返回
The other possibility to avoid is to declare what you want to return as static and just use return by reference