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