public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/40295] New: [C++0x] std::move and builtin types
@ 2009-05-29 16:43 jwakely dot gcc at gmail dot com
2009-05-29 17:08 ` [Bug c++/40295] [C++0x] rvalue-references " jwakely dot gcc at gmail dot com
2009-06-02 14:14 ` jwakely dot gcc at gmail dot com
0 siblings, 2 replies; 3+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2009-05-29 16:43 UTC (permalink / raw)
To: gcc-bugs
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2162 bytes --]
I'm not sure what's going on here, but with a recent 4.5 snapshot this program
fails:
#include <utility>
#include <cassert>
template<typename T>
struct S
{
S() : buf() { }
void set(T&& i) { buf = std::move(i); }
T&& get() { return std::move(buf); }
private:
T buf;
};
struct pod { };
template class S<int>;
template class S<double>;
template class S<pod>;
int main()
{
S<int> s;
s.set(5);
assert( s.get() == 5 );
}
Compiling with 4.5.0 20090528 gives these warnings:
tmpret.cc: In member function T&& S<T>::get() [with T = int]:
tmpret.cc:19: instantiated from here
tmpret.cc:11: warning: returning reference to temporary
tmpret.cc: In member function T&& S<T>::get() [with T = double]:
tmpret.cc:20: instantiated from here
tmpret.cc:11: warning: returning reference to temporary
tmpret.cc: In function int main():
tmpret.cc:27: warning: <anonymous> is used uninitialized in this function
And the assertion fails at runtime.
Notice that the explicit instantiations of S<int> and S<double> trigger the
warning about returning a reference to a temporary for this line:
T&& get() { return std::move(buf); }
but the S<pod> instantiation doesn't. I can't see what's wrong with that line,
it seems that std::move creates a temporary when 'T' is a builtin type.
It looks as though std::move(buf) instantiates std::move<T>(T&&) with T = int&,
so the rvalue-reference binds to an int& not an int.
Am I missing something or is this a bug in either std::move or g++?
N.B. the changes to std::move proposed by n2844 are not in the WP yet, so the
specification of std::move may change.
--
Summary: [C++0x] std::move and builtin types
Product: gcc
Version: 4.5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jwakely dot gcc at gmail dot com
GCC build triplet: x86_64-unknown-linux-gnu
GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40295
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c++/40295] [C++0x] rvalue-references and builtin types
2009-05-29 16:43 [Bug libstdc++/40295] New: [C++0x] std::move and builtin types jwakely dot gcc at gmail dot com
@ 2009-05-29 17:08 ` jwakely dot gcc at gmail dot com
2009-06-02 14:14 ` jwakely dot gcc at gmail dot com
1 sibling, 0 replies; 3+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2009-05-29 17:08 UTC (permalink / raw)
To: gcc-bugs
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1214 bytes --]
------- Comment #1 from jwakely dot gcc at gmail dot com 2009-05-29 17:08 -------
This is independent of std::move
#include <cassert>
struct S
{
S() : buf(5) { }
int&& get() { return static_cast<int&&>(buf); }
int buf;
};
int main()
{
S s;
int&& r = s.get();
assert( &r == &s.buf );
}
tmpret.cc: In member function int&& S::get():
tmpret.cc:7: warning: returning reference to temporary
tmpret: tmpret.cc:16: int main(): Assertion `&r == &s.buf' failed.
Looks like the cast to int&& creates a temporary, which is bound to the
returned reference, triggering the warning. According to 8.5.3 [dcl.init.ref]
paragraph 5, the reference should be bound to the object, not to a temporary.
--
jwakely dot gcc at gmail dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|libstdc++ |c++
Keywords| |wrong-code
Summary|[C++0x] std::move and |[C++0x] rvalue-references
|builtin types |and builtin types
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40295
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c++/40295] [C++0x] rvalue-references and builtin types
2009-05-29 16:43 [Bug libstdc++/40295] New: [C++0x] std::move and builtin types jwakely dot gcc at gmail dot com
2009-05-29 17:08 ` [Bug c++/40295] [C++0x] rvalue-references " jwakely dot gcc at gmail dot com
@ 2009-06-02 14:14 ` jwakely dot gcc at gmail dot com
1 sibling, 0 replies; 3+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2009-06-02 14:14 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from jwakely dot gcc at gmail dot com 2009-06-02 14:14 -------
*** This bug has been marked as a duplicate of 34022 ***
--
jwakely dot gcc at gmail dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |DUPLICATE
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40295
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-06-02 14:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-29 16:43 [Bug libstdc++/40295] New: [C++0x] std::move and builtin types jwakely dot gcc at gmail dot com
2009-05-29 17:08 ` [Bug c++/40295] [C++0x] rvalue-references " jwakely dot gcc at gmail dot com
2009-06-02 14:14 ` jwakely dot gcc at gmail dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).