Constructors for priority_queue versus queue and stack

Constructors for priority_queue versus queue and stack

Post by Edward Die » Wed, 10 Mar 2010 11:10:15

For the priority_queue container adapter one of the constructors takes
a pair of InputIterators ( along with compare and container parameters
). The queue and stack container adapters do not have such a

What is the reason for limiting the queue and stack's constructor in this way ?

Constructors for priority_queue versus queue and stack

Post by Daniel Krl » Wed, 10 Mar 2010 14:24:13

I do not think that this is a limitation of the latter, but
that the design choice for priority_queue was based
on the history of the STL.
Before move-semantics was added to C++, it was
probably considered to add the range constructors,
because compilers were not that good in inlining code
in these days. priority_queue is particularly different
from the other adaptors, because it is the only one,
that combines another container with an additional
component, the comparer. In this case you cannot
accept the order of elements that would be provided
by an already filled source container, but you must
resort that in any case. Therefore it was probably
assumed that this special constructor would
allow for better optimization choices of this special
situation. Note that priority_queue really deviates
from the basic principles of the adaptor classes
in this regard looking at the first paragraph of

"The container adaptors each take a Container
template parameter, and each constructor takes
a Container reference argument. [..]."

Therefore I don't really consider it as a defect that
the other adaptors do not provide range c'tors.

HTH & Greetings from Bremen,

Daniel Krler

[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: ]