Why std queue does not define swap method specialization

I read that all stl containers provide swap algorithm specialization to avoid calling the copy constructor and the two assignment operations that the default method uses. However, when I thought it would be nice to use the queue in some code that I was working on, I noticed that (unlike the vector and deck) the queue does not provide this method? I just decided to use deque instead of the queue, but I'm still interested to know why this is?

+4
source share
2 answers

C ++ 0x will add swap to container adapters like std :: queue. I could only guess why it is not in the current standard. In this discussion, someone offers a workaround:

There is a solution because the standard protects the necessary parts, called inheritance. [just do not destroy it with std adapters] create a template structure that inherits the desired adapter, constructors and redirect the arguments to the adapter class, writing a swap-member is a snap, since the required elements are protected by standard adapters.

+3
source

I am sure that they were excluded due to oversight. In fairness, I often use std :: queue and std :: stack, and I never had to change them. I think your use of the deck instead of the queue is fine. Something like typedef std::deque<MyType> QueueType should give enough clues on how to use the container.

+1
source

All Articles