Boost logo

Boost Users :

Subject: Re: [Boost-users] [foreach] gcc 4.5 & 4.6 compiler error with BOOST_FOREACH(auto&& ...) loop
From: Michel MORIN (mimomorin_at_[hidden])
Date: 2011-03-05 09:03:13


Sebastian Redl wrote:
>> template<typename>
>> void f()
>> {
>> int A[6];
>> for(auto&& x : A)
>> ;
>> }
>>
>> Why does this compile? I don't see how any rvalue can be available in that
>> context.
>
> Wouldn't auto deduction make x an lvalue reference here?

Yes, x should be an lvalue reference here. The code should compile fine.

I think this is a gcc bug.
In a function template, auto deduction fails to deduce an lvalue reference
when auto&& is initiliazed by a function return value of an lvalue reference.

Minimal test case:
(This code fails to be compiled.)

int& identity(int& i)
{
    return i;
}

template <typename = void> // Commenting this out makes compilation succeed
void f()
{
    int i = 0;
    auto&& x = identity(i); // In a function template, auto deduction fails
}

int main (int argc, char* argv[])
{
    f();
    return 0;
}

Regards,
Michel


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net