given the following code:
#include <set>
using std::set;
class Pool {
set<int> s;
public:
typedef typename set<int>::iterator Iterator;
const Iterator& begin() const {
return s.begin(); //** error
}
};
Why I get the following error (I understand the meaning of the error, but, I don't understand why I get it in this case)?
returning reference to temporary [-Werror=return-local-addr]
How can I fix it?
The set<...>::begin
function returns its iterator by value. Since you do not store that value anywhere, it's a temporary value, and you can't really have references to temporary values.
The simple solution is for your function to also return by (non-const) value.