Search code examples
c++stlqueueswapstl-algorithm

Why does std queue not define a swap method specialisation


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


Solution

  • C++0x will add swap to container adapters like std::queue. I could only speculate why it is missing from the current standard. In this discussion someone proposes a workaround:

    There is a solution since the standard makes the needed parts protected, called inheritance. [just don't destruct via the std adaptors] create a templated struct inheriting the desired adaptor, provide just the constructors and forward the args to the adaptor class, writing a swap member is a snap since the required items are protected members of the standard adaptors.