pop_back() return value?

C++Vector

C++ Problem Overview


Why doesn't pop_back() have a return value? I have Googled regarding this and found out that it makes it more efficient. Is this the only reason for making it so in the standard?

C++ Solutions


Solution 1 - C++

Efficiency has little (or nothing, really) to do with it.

This design is the outcome of an important paper by Tom Cargill, published in the 90s, that raised quite a few eyebrows back then. IIRC, in it Cargill showed that it is impossible to design an exception safe stack pop function.

Solution 2 - C++

I think there is something related to the fact that copying an instance of the last object could throw an exception. When doing so, you're losing your object, since pop_back() did remove it from your container. Better with a few lines of code:

std::vector<AnyClass> holds = {...} ;
try {
  const AnyClass result = holds.pop_back(); // The copy Ctor throw here!
} catch (...)
{ 
 // Last value lost here. 
}

Solution 3 - C++

Solution 4 - C++

Efficiency is one thing. Another reason for pop_back() not returning an element is exception safety.
If the pop() function returned the value, and an exception is thrown by the copy constructor, you may not be able to guarantee that the container is in the same state as it was before calling pop().

You can find more infos in Herb Sutters books about exceptions. I think this topic is covered here. But I am not sure.

Solution 5 - C++

The reason is not so much efficiency as exception safety. The container class can be used to store any kind of objects. It would be impossible to implement pop_back() in an exception safe manner if the function would return the object after deleting it from the container, because returning the value of the object involves copy construction.

This is the actual implementation of vector::pop_back() in GNU C++ standard library:

  void
  pop_back()
  {
    --this->_M_impl._M_finish;
    this->_M_impl.destroy(this->_M_impl._M_finish);
  }

This is what it would look like should it return the last element in the end:

  value_type
  pop_back()
  {
    value_type save = back();
    --this->_M_impl._M_finish;
    this->_M_impl.destroy(this->_M_impl._M_finish);
    return save;
  }

This involves two copy constructions, at the save = back() statement and when returning a copy of the object. There are no guarantees that the return expression won't throw an exception after the element has been destroyed from the container.

Solution 6 - C++

Well, how many reasons there have to be?

This avoids potentially expensive copying of the object when you just want to remove it from the container. C++ has the philosophy of not paying for what you don't need.

Solution 7 - C++

> In computer programming, orthogonality means that operations change > just one thing without affecting others.

pop_back() does just one thing, it does not copy, hence it is orthogonal.

Solution 8 - C++

Why would it return the value? You can always access the value at any time prior to popping it off- there is no need for pop_back to provide this functionality.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParvinder SinghView Question on Stackoverflow
Solution 1 - C++sbiView Answer on Stackoverflow
Solution 2 - C++yves BaumesView Answer on Stackoverflow
Solution 3 - C++AbyxView Answer on Stackoverflow
Solution 4 - C++mkaesView Answer on Stackoverflow
Solution 5 - C++Seppo EnarviView Answer on Stackoverflow
Solution 6 - C++Zdeslav VojkovicView Answer on Stackoverflow
Solution 7 - C++user1095108View Answer on Stackoverflow
Solution 8 - C++PuppyView Answer on Stackoverflow