public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference
@ 2011-10-31 5:42 jarrydb at cse dot unsw.edu.au
2011-10-31 7:16 ` [Bug c++/50929] " daniel.kruegler at googlemail dot com
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: jarrydb at cse dot unsw.edu.au @ 2011-10-31 5:42 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
Bug #: 50929
Summary: [C++0x] Wrong function selected for overload with
template and rvalue reference
Classification: Unclassified
Product: gcc
Version: 4.7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: jarrydb@cse.unsw.edu.au
When choosing between a constructor
A(const A& a)
and
template <typename T>
A(T&& t)
given an object of type A&, the compiler chooses the second function. For the
following code:
class A
{
public:
A() : value(0) {}
A(const A& rhs) : value(1) { }
A(A&& rhs) : value(2) { }
template <typename T>
A(T&& t) : value(3) { }
int value;
};
int main()
{
A a;
A b = a;
return b.value;
}
compiled with:
g++ -std=gnu++0x rvalue_deduction.cpp
then running:
./a.out
echo $?
the output is
3
I expect the output to be 1, not 3.
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/jarrydb/current/soft/install-latest/libexec/gcc/x86_64-unknown-linux-gnu/4.7.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /home/jarrydb/current/soft/src/gcc-git/configure
--prefix=/home/jarrydb/current/soft/install-latest --disable-multilib
--enable-languages=c,c++
Thread model: posix
gcc version 4.7.0 20111027 (experimental) (GCC)
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
@ 2011-10-31 7:16 ` daniel.kruegler at googlemail dot com
2011-10-31 7:43 ` jarrydb at cse dot unsw.edu.au
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2011-10-31 7:16 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
Daniel Krügler <daniel.kruegler at googlemail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |daniel.kruegler at
| |googlemail dot com
--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2011-10-31 07:15:48 UTC ---
Your expectations are in violation to the standard ;-). The constructor is
selected by overload resolution. The non-template constructor A(const A& a)
would be preferred, if there would be a perfect match (e.g. if you had an
lvalue of type const A), but in your example you have an lvalue of non-const A.
The "perfect-forwarding" template constructor deduces to
template <> A(A& t)
via reference-collapsing, which is a better match for the argument expression
a.
So, this looks like an invalid issue to me.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
2011-10-31 7:16 ` [Bug c++/50929] " daniel.kruegler at googlemail dot com
@ 2011-10-31 7:43 ` jarrydb at cse dot unsw.edu.au
2011-10-31 8:25 ` daniel.kruegler at googlemail dot com
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: jarrydb at cse dot unsw.edu.au @ 2011-10-31 7:43 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
--- Comment #2 from Jarryd Beck <jarrydb at cse dot unsw.edu.au> 2011-10-31 07:42:54 UTC ---
That's a shame, and rather annoying. I read pages and pages of the standard to
try to understand this one. Do you know where this is explained?
I added a non-const constructor and also tried it with a const object, they
worked as you said.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
2011-10-31 7:16 ` [Bug c++/50929] " daniel.kruegler at googlemail dot com
2011-10-31 7:43 ` jarrydb at cse dot unsw.edu.au
@ 2011-10-31 8:25 ` daniel.kruegler at googlemail dot com
2011-10-31 10:10 ` jarrydb at cse dot unsw.edu.au
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2011-10-31 8:25 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
--- Comment #3 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2011-10-31 08:24:58 UTC ---
(In reply to comment #2)
> That's a shame, and rather annoying. I read pages and pages of the standard to
> try to understand this one. Do you know where this is explained?
Actually this is already required in C++03 (modulo perfect forwarding), see
[class.copy], footnote 106:
"Because a template constructor is never a copy constructor, the presence of
such a template does not suppress the implicit declaration of a copy
constructor. Template constructors participate in overload resolution with
other constructors, including copy constructors, and a template constructor may
be used to copy an object if it provides a better match than other
constructors."
In C++11 this is just part of the general description of initialization
semantics in N3290, [dcl.init] p16.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
` (2 preceding siblings ...)
2011-10-31 8:25 ` daniel.kruegler at googlemail dot com
@ 2011-10-31 10:10 ` jarrydb at cse dot unsw.edu.au
2011-10-31 10:18 ` daniel.kruegler at googlemail dot com
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: jarrydb at cse dot unsw.edu.au @ 2011-10-31 10:10 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
--- Comment #4 from Jarryd Beck <jarrydb at cse dot unsw.edu.au> 2011-10-31 10:07:04 UTC ---
I didn't realise that A(A&) was a better match. I was thinking of C++ code
where you might write:
template <typename T>
A(const T& t);
A(const A& rhs);
in which case A(const A&) would be chosen. I didn't realise that the rvalue
reference puts a spanner in the works. I was reading the part about a template
function and a non-template function being candidates, and if the non-template
function is no worse than the template function then it is chosen. But I didn't
realise that in this case the non-template one is actually worse.
I suppose that the bug can probably be marked as invalid then.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
` (3 preceding siblings ...)
2011-10-31 10:10 ` jarrydb at cse dot unsw.edu.au
@ 2011-10-31 10:18 ` daniel.kruegler at googlemail dot com
2011-10-31 10:34 ` jarrydb at cse dot unsw.edu.au
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2011-10-31 10:18 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
--- Comment #5 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2011-10-31 10:18:04 UTC ---
(In reply to comment #4)
> I didn't realise that A(A&) was a better match. I was thinking of C++ code
> where you might write:
>
> template <typename T>
> A(const T& t);
>
> A(const A& rhs);
>
> in which case A(const A&) would be chosen.
Sure. But if you had provided
template <typename T>
A(T& t);
instead that would again be a better match for a non-const A lvalue.
Non-template functions only win over templates, if they are otherwise equal in
matching.
> I didn't realise that the rvalue reference puts a spanner in the works.
Note that "rvalue-reference" is a red herring for the perfect-forwarding
signature
template <typename T>
A(T&& t);
It depends on whether the argument is an lvalue or not to decide whether the
finally deduced type is effectively A& or A&& (or whatever argument type had
been provided). In your example the argument was an lvalue of type A, therefore
the deduced signature was effectively
template <> A(A& t);
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
` (4 preceding siblings ...)
2011-10-31 10:18 ` daniel.kruegler at googlemail dot com
@ 2011-10-31 10:34 ` jarrydb at cse dot unsw.edu.au
2011-10-31 10:41 ` paolo.carlini at oracle dot com
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: jarrydb at cse dot unsw.edu.au @ 2011-10-31 10:34 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
--- Comment #6 from Jarryd Beck <jarrydb at cse dot unsw.edu.au> 2011-10-31 10:34:16 UTC ---
Yeah I understand now. Basically having a perfect forwarding constructor means
that I need a non-const constructor if I want my example to work as I initially
said.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
` (5 preceding siblings ...)
2011-10-31 10:34 ` jarrydb at cse dot unsw.edu.au
@ 2011-10-31 10:41 ` paolo.carlini at oracle dot com
2011-10-31 11:46 ` redi at gcc dot gnu.org
2011-10-31 12:20 ` paolo.carlini at oracle dot com
8 siblings, 0 replies; 10+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-10-31 10:41 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
--- Comment #7 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-10-31 10:40:41 UTC ---
Thanks Daniel, let's close this then.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
` (6 preceding siblings ...)
2011-10-31 10:41 ` paolo.carlini at oracle dot com
@ 2011-10-31 11:46 ` redi at gcc dot gnu.org
2011-10-31 12:20 ` paolo.carlini at oracle dot com
8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2011-10-31 11:46 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-10-31 11:45:32 UTC ---
this is a duplicate of PR 46004
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/50929] [C++0x] Wrong function selected for overload with template and rvalue reference
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
` (7 preceding siblings ...)
2011-10-31 11:46 ` redi at gcc dot gnu.org
@ 2011-10-31 12:20 ` paolo.carlini at oracle dot com
8 siblings, 0 replies; 10+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-10-31 12:20 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50929
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|INVALID |DUPLICATE
--- Comment #9 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-10-31 12:19:51 UTC ---
*** This bug has been marked as a duplicate of bug 46004 ***
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2011-10-31 12:20 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-31 5:42 [Bug c++/50929] New: [C++0x] Wrong function selected for overload with template and rvalue reference jarrydb at cse dot unsw.edu.au
2011-10-31 7:16 ` [Bug c++/50929] " daniel.kruegler at googlemail dot com
2011-10-31 7:43 ` jarrydb at cse dot unsw.edu.au
2011-10-31 8:25 ` daniel.kruegler at googlemail dot com
2011-10-31 10:10 ` jarrydb at cse dot unsw.edu.au
2011-10-31 10:18 ` daniel.kruegler at googlemail dot com
2011-10-31 10:34 ` jarrydb at cse dot unsw.edu.au
2011-10-31 10:41 ` paolo.carlini at oracle dot com
2011-10-31 11:46 ` redi at gcc dot gnu.org
2011-10-31 12:20 ` paolo.carlini at oracle 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).