Bug 47999 - [C++0x] auto type deduction works incorrectly in a function template
Summary: [C++0x] auto type deduction works incorrectly in a function template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: 4.6.1
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-05 17:28 UTC by Michel Morin
Modified: 2011-03-29 14:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-03-08 13:47:09


Attachments
Test case (205 bytes, application/octet-stream)
2011-03-05 17:28 UTC, Michel Morin
Details
Fix (769 bytes, patch)
2011-03-08 15:14 UTC, Jason Merrill
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michel Morin 2011-03-05 17:28:38 UTC
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.)
Comment 1 Paolo Carlini 2011-03-05 23:39:18 UTC
Let's CC Jason,
Comment 2 Jason Merrill 2011-03-08 15:14:02 UTC
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.
Comment 3 Jason Merrill 2011-03-16 17:04:35 UTC
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
Comment 4 Jason Merrill 2011-03-29 14:25:49 UTC
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
Comment 5 Jason Merrill 2011-03-29 14:38:30 UTC
Fixed for 4.6.1.