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?

- 8,805
- 2
- 43
- 70

- 1,737
- 2
- 12
- 17
8 Answers
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.
-
5+1 for the reference to Tom Cargill paper "Excpetion handling: a false sense of security". A MUST-READ. :-) – yves Baumes Sep 26 '12 at 11:19
-
2If I remember correctly then “The C++ Language” or some edition of “Effective C++” actually claimed that it was for efficiency reasons because they hadn’t figured out move construction and NRVO then. – Konrad Rudolph Sep 26 '12 at 12:24
-
@Konrad: AFAIR, Meyers even pointed at Cargill's paper. (Come to think of it, I bet that's the actual reason why a copy of it exists, at Pearson's website.) – sbi Sep 26 '12 at 12:28
-
How can we be sure that a removed object does not throw in destructor? I understand that it shouldn't, but how can we control it? – Mikhail Apr 28 '14 at 09:15
-
@Mikhail: The same way you can be sure that some function does what the spec says: You need to trust those who wrote the code that they didn't do anything stupid. – sbi Apr 29 '14 at 22:03
-
@sbi So we guarantee `pop_back` doesn't throw unless user is stupid? – Mikhail Apr 30 '14 at 06:28
-
7@Mikhail: If destructors throw, you cannot guarantee anything. That's the reason they must not. Again: ___Destructors must never throw. Period.___ If you write a dtor that lets escape an exception, you might just as well dereference `NULL`. – sbi May 05 '14 at 14:14
-
Interesting paper. However I don't immediately see that the paper shows it's **_impossible_** to design an exception-safe pop function. For example isn't it possible to copy the last element into a parameter passed by reference, e.g. void Stack::pop(T &oldTop), such that if this copy throws, the stack remains unchanged? – egyik Dec 09 '18 at 14:17
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.
}

- 501
- 2
- 10

- 8,836
- 7
- 45
- 74
-
14+1 `T container
::pop()`cannot be implemented exception safe (http://www.gotw.ca/gotw/008.htm) – hansmaad Sep 26 '12 at 11:15 -
6+1 this is the correct answer. It tells us the underlying principle which led to this design of `pop_back` with no return value. – Nawaz Sep 26 '12 at 11:15
-
You wouldn't copy it, you would *move* it. That's a whole different ball game. – Puppy Sep 26 '12 at 19:46
-
1Right. I am not really (yet) knowledgeable with c++11. But I guess the answer still holds true. I mean, not with a compiler-provided move ctor, but a user-defined one. Moreover, afaik, I remember reading somewhere that a class may be defined to eludes any move ctor (= delete?). If so, then we fall back in the copy ctor case (if such is provided, otherwise I guess using a vector won't compile). – yves Baumes Sep 26 '12 at 20:12
-
1@DeadMG move constructors might not be `noexcept` either, if I'm not mistaken – sehe Sep 26 '12 at 22:28
-
2@DeadMG: *move* or *copy*, that depends on the *type* of value, as not every type is movable, plus *move* might throw exception as well. – Nawaz Sep 27 '12 at 10:14
-
@hansmaad How can we be sure that a removed object does not throw in destructor? I understand that it shouldn't, but how can we control it? – Mikhail Apr 28 '14 at 09:29
-
When discussing exception safety you need to consider the kind of safety you guarantee (https://en.wikipedia.org/wiki/Abrahams_guarantees). As I understand, the convention of avoiding element destruction and copy construction in the same pop method offers the possibility of "strong" exception safety (no data loss). However, STL does not make such guarantees in general, so IMHO they could very well have added a pop method that returns a copy (still guaranteeing "basic" safety, i.e. no leaks). Am I wrong? – Malström May 26 '14 at 12:14
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.

- 13,781
- 10
- 52
- 72
-
4The problem isn't that the container is changed, the problem is that the object might be removed, but you haven't gotten it back — so it's lost. – sbi Sep 26 '12 at 11:20
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.

- 3,219
- 3
- 32
- 25
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.

- 14,391
- 32
- 45
-
I believe this is wrong. See [my comment to Ravindra's answer](http://stackoverflow.com/questions/12600330/pop-back-return-value#comment16986861_12600397). – sbi Sep 26 '12 at 13:31
-
3well, I am quite sure that exception related issue is likey *one* of the reasons, but I am also quite sure that this is another. I have yet to see any evidence that this design has nothing to do with performance issue (as move semantics is quite a new thing) and I haven't claimed there isn't another (OK, my first sentence might sound like that, but it was meant to clarify why one reason wouldn't be enough). Sutter claims in "Exceptional C++" that "here's *one* reason to do this: It avoids weakening exception safety.". – Zdeslav Vojkovic Sep 26 '12 at 16:36
-
2"I have yet to see any evidence that this design has nothing to do with performance issue." You also have yet to see any evidence that this design has nothing to do with bananas. _So?_ It's you who made a claim ("This is about performance!"), and it's me who challenged that. So it's you who need to back up the claim. – sbi Sep 26 '12 at 22:00
-
3ok, you got me working there :) Stroustrup says in 'C++ prog. lang' [16.3.5]: `It just pops, and if we want to know what was on the top of the stack before pop, we must look. This happens not to be my favorite style of stack, but it's arguably more efficient and it's the standard`. OTOH, Josuttis book also refers to Cargill paper. My conclusion is that performance _is_ one of the reasons, while I don't claim that it is the only one. However, upon reading all the references I would also agree that exception-safety might have been more important factor in deciding this way. – Zdeslav Vojkovic Sep 27 '12 at 07:22
-
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.

- 14,119
- 9
- 58
- 116
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.

- 144,682
- 38
- 256
- 465
-
1-1. It doesn't answer the question. There is a rationale why `pop_back` doesn't return value, and this post doesn't talk about that rationale. – Nawaz Sep 26 '12 at 11:16
-
1The fact that there is no reason for it to exist is an excellent reason for it to not exist, and that does answer the question. – Puppy Sep 26 '12 at 11:17
-
6@DeadMG: I believe that there's a good usability case pro `pop_back()` returning a value. Traditionally, that's what a stack's pop function does. No, the reason is purely for exception safety. – sbi Sep 26 '12 at 13:56