Boost logo

Boost Users :

Subject: Re: [Boost-users] [foreach] gcc 4.5 & 4.6 compiler error with BOOST_FOREACH(auto&& ...) loop
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2011-03-05 08:11:12


On 05/03/2011 03:13, Nathan Ridge wrote:

> 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'

Simply put, the value it is initialized with is an lvalue.

>
> Interestingly, the error goes away if I make f() a non-template function. It also compiles fine with gcc 4.4.

GCC 4.4 allowed lvalues to bind to rvalue references, which is why.

> 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)
> ;
> }

Why does this compile? I don't see how any rvalue can be available in
that context.


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