Summary: | [C++0x] auto type deduction works incorrectly in a function template | ||
---|---|---|---|
Product: | gcc | Reporter: | Michel Morin <mimomorin> |
Component: | c++ | Assignee: | Jason Merrill <jason> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | jason, zeratul976 |
Priority: | P3 | ||
Version: | 4.6.0 | ||
Target Milestone: | 4.6.1 | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | 2011-03-08 13:47:09 | |
Attachments: |
Test case
Fix |
Let's CC Jason, Created attachment 23583 [details]
Fix
Here's a patch that fixes the bug and passes regression testing. I don't plan to put it in for 4.6.0 as I'm concerned about possible side-effects; I seem to remember trying to do exactly this once before and it causing problems. I'll put it in for 4.7 once 4.6 branches, and then perhaps into 4.6.1.
Author: jason Date: Wed Mar 16 17:04:30 2011 New Revision: 171053 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171053 Log: PR c++/47999 * semantics.c (finish_call_expr): Preserve reference semantics in templates. Added: trunk/gcc/testsuite/g++.dg/cpp0x/auto22.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/semantics.c trunk/gcc/testsuite/ChangeLog Author: jason Date: Tue Mar 29 14:25:37 2011 New Revision: 171670 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171670 Log: PR c++/47999 * semantics.c (finish_call_expr): Preserve reference semantics in templates. Added: branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/auto22.C Modified: branches/gcc-4_6-branch/gcc/cp/ChangeLog branches/gcc-4_6-branch/gcc/cp/semantics.c branches/gcc-4_6-branch/gcc/testsuite/ChangeLog Fixed for 4.6.1. |
Created attachment 23554 [details] Test case When `auto&&` is initialized with a function return value that is an lvalue reference, the deduced type should be an lvalue reference. int& identity(int& i) { return i; } void f() { int i = 0; auto&& x = identity(i); // x has type `int&` } But, in a function template, auto type deduction incorrectly deduces the type as an rvalue reference. template <typename T> void f() { int i = 0; auto&& x = identity(i); // BUG: x has type `int&&` } This bug leads to compilation error in gcc 4.5 and 4.6, because lvalues cannot bind to rvalue references. In gcc 4.4, the code compiles fine because lvalues are allowed to bind to rvalue references. Tested compilers: gcc-4.4.5, 4.5.2, 4.6-20110219. (This problem was found by Nathan Ridge.)