[LLVMbugs] [Bug 13060] New: Need warning for returning a C++11 rvalue reference to a local
bugzilla-daemon at llvm.org
bugzilla-daemon at llvm.org
Fri Jun 8 13:05:36 PDT 2012
http://llvm.org/bugs/show_bug.cgi?id=13060
Bug #: 13060
Summary: Need warning for returning a C++11 rvalue reference to
a local
Product: clang
Version: trunk
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P
Component: Frontend
AssignedTo: unassignedclangbugs at nondot.org
ReportedBy: arthur.j.odwyer at gmail.com
CC: llvmbugs at cs.uiuc.edu
Classification: Unclassified
(I've stolen the formatting from Bug 12566, but this is a different issue.)
If I understand the semantics of rvalue references correctly, then the
following code should produce a warning for returning a reference to the local
s in foo():
std::string &&foo() {
std::string s = "hello";
return std::move(s);
}
similar to the warning given when returning an lvalue reference to a local:
warning: reference to stack memory associated with local variable
'n' returned [-Wreturn-stack-address]
return &n;
The reason is that the return value --- *if* I understand rvalue references
correctly --- is going to be an xvalue to a variable ("s") that has already
expired. Even if the caller immediately moves the result of foo() into a new
location using a move-constructor or move-assignment operator, it's too late.
It's too late as soon as we leave the scope of foo() and local variable "s" is
destructed.
Am I wrong about the semantics of the rvalue reference in this code? Am I right
but there is a good technical reason *not* to give a warning? Or is this a good
warning to give, but nobody's gotten around to it yet?
Incidentally, I believe that returning an rvalue reference to a reference
*parameter* is hunky-dory.
std::string &&okay(std::string &&trashme) {
trashme = "hello";
return trashme; // This should NOT give the same warning,
// although it is allegedly still dangerous.
// See http://stackoverflow.com/questions/6006527
}
--
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.
More information about the llvm-bugs
mailing list