Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Why I get the following error: "returning reference to temporary [-Werror=return-local-addr]"

Ask Question 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?

@Someprogrammerdude I know that it's will fix the error (but I don't understand why I can't return by-reference in this case) MathQues Jun 26, 2018 at 15:06 by not returning a reference to a temporary. s.begin() creates a temporary object and you return a reference to it. Iterators are usually passed by value as they are cheap to copy Alan Birtles Jun 26, 2018 at 15:06 @AlanBirtles So I can't return object from my set by reference (If I want to change it from the outside) ? MathQues Jun 26, 2018 at 15:07 You can return a reference to an object (subject to the iterator invalidation rules) but you can't return an iterator by reference, you must return by value. Alan Birtles Jun 26, 2018 at 15:13

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.

@SombreroChicken You right, but in my class exists more method that called begin and returns non-const by-reference (so, my question now it's about this method) MathQues Jun 26, 2018 at 15:13 @MathQueus, the iterator internally references set<int> s, so returning by value and using the iterator, you are still working on 's' rmawatson Jun 26, 2018 at 15:17 @MathQues, the iterator doesn't hold the value that is in the set, it is just a lightweight type for iterating over the set. Internally holding some kind of reference to the set that you are iterating over. When you dereference the iterator is when it accesses the set & its value. rmawatson Jun 26, 2018 at 15:43

is illegal because the result of s.begin() is a temporary object and therefore you can't return a reference to it. Maybe if you look at this equivalent code it will be clearer?

const Iterator& begin() const {
  Iterator it = s.begin();
  return it; //** error

Returning a const reference to an iterator doesn't make much sense anyway as you wont be able to move the location that the iterator points to. You should always return iterators by value, this way the caller is free to make copies of the iterator and modify their location. For example if you could get your code to compile the following calling code would not work:

Pool p;
const Pool::Iterator& it = p.begin();
++it; //error it is constant

The user could fix their code by copying your returned reference into a new object:

Pool p;
Pool::Iterator it = p.begin();
++it; //no error

As your users wont be able to use the reference you can't return its better just to return by value:

const Iterator begin() const {
  return s.begin();

Note that std::set is not like most other containers and doesn't allow modification of the values via its iterators: https://en.cppreference.com/w/cpp/container/set/begin

Because both iterator and const_iterator are constant iterators (and may in fact be the same type), it is not possible to mutate the elements of the container through an iterator returned by any of these member functions.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.