std::deque.pop_front & pop_back

B

bb

Hi,

Is there any specific reason why std::deque's pop_front & pop_back
does not return the removed object?

It could return the removed object by value (possibly atomically?)

Cheers.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,

Is there any specific reason why std::deque's pop_front & pop_back
does not return the removed object?

It could return the removed object by value (possibly atomically?)

Exception safety. If pop did remove the object and for some reason an
exception was thrown while creating the copy you could end up loosing
an object. Can't remember the exact details right now but some
googling should help.
 
M

mliptak

Hi,

Is there any specific reason why std::deque's pop_front & pop_back
does not return the removed object?

It could return the removed object by value (possibly atomically?)

Cheers.

The SGI's docs contain the reasoning - it's from queue's documentation
but it also applies for deque:
"One might wonder why pop() returns void, instead of value_type. That
is, why must one use front() and pop() to examine and remove the
element at the front of the queue, instead of combining the two in a
single member function? In fact, there is a good reason for this
design. If pop() returned the front element, it would have to return
by value rather than by reference: return by reference would create a
dangling pointer. Return by value, however, is inefficient: it
involves at least one redundant copy constructor call. Since it is
impossible for pop() to return a value in such a way as to be both
efficient and correct, it is more sensible for it to return no value
at all and to require clients to use front() to inspect the value at
the front of the queue."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Chatbot 0
std::list wrapper (+templates) 8
ADA Compliance/Web Accessibility Advice 1
Indexed list 0
string vs. vector<char> 6
std::list<T>::iterator 12
Creating a vector of queues 2
Deriving from STL containers 6

Members online

No members online now.

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top