Boost logo

Boost Users :

Subject: [Boost-users] [foreach] gcc 4.5 & 4.6 compiler error with BOOST_FOREACH(auto&& ...) loop
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-03-04 21:13:28


Hello,
 
The following code fails to compile with gcc 4.5 or 4.6:
 
#include <boost/foreach.hpp>

template <typename>
void f()
{
    int A[6];
    BOOST_FOREACH(auto&& x, A)
        ;
}

void g()
{
    f<int>();
}

The error is:
 
test.cpp: In function 'void f() [with <template-parameter-1-1> = int]':
test.cpp:14:12: instantiated from here
test.cpp:6:5: error: invalid initialization of reference of type 'int&&' from expression of type 'int'
 
Interestingly, the error goes away if I make f() a non-template function. It also compiles fine with gcc 4.4.
 
 
The native range-based for version, however, compiles fine with gcc 4.6 (which is the only version to support it):
 
#include <boost/foreach.hpp>

template <typename>
void f()
{
    int A[6];
    for(auto&& x : A)
        ;
}

void g()
{
    f<int>();
}
 
 
What is going on here? Is this a Boost issue or a gcc issue?
 
 
Thanks,
Nate.


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