c++ - BOOST_FOREACH iterating rvalue container has error with non const ref value -


i'm using g++ 4.4.7 20120313

class obj { int a; }  std::list<obj> list;  boost_foreach(obj& v, list) { } // ok     boost_foreach(const obj& v, list) { } // ok      std::list<obj> getlist() { ... }  boost_foreach(obj& v, getlist()) { } // error: invalid initialization of reference of type 'obj&' expression of type 'const obj' boost_foreach(const obj& v, getlist()) {} // ok 

why got error? why should use const obj& instead of obj& ?

we can simplify following:

int f() { return 99 ; } int main() {     const int& p = f() ; // ok     int& p = f() ;       // error: invalid initialization of non-const reference                          // of type ‘int&’ rvalue of type ‘int’     return 0 ; } 

does make more sense you?


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -