Le 26/05/13 08:42, Diego R. a �crit :
> Priority queues cannot be used with types non-copyable. Because top()
> returns a constant reference. For example I can't create something
> like priority_queue<std::unique_ptr<MyPolymorphicClass>>.
>
> I propose that priority_queue have more support for move semantics by
> using of new member functions. Additionaly I would add these new
> members functions to stack and queue for simmetry.
>
> Below I show an idea.
>
> template <class T, class Container, class Comparer>
> class priority_queue
> {
> ...
> value_type move_top()
> {
> value_type v;
> pop(v);
> return v;
> }
>
> void pop(value_type& v)
> {
> std::pop_heap(c.begin(), c.end(), comp);
> v = std::move(c.back());
> c.pop_back();
> }
> };
>
Yes, there is an issue here. I guess top() returns a const_reference to
ensure the class invariants.
Maybe pull() could be used to name both operations.
> template <class T, class Container>
> class stack
> {
> ...
> value_type move_top()
> {
> value_type v;
> pop(v);
> return v;
> }
>
> void pop(value_type& v)
> {
> v = std::move(c.back());
> c.pop_back();
> }
> };
>
> template <class T, class Container>
> class queue
> {
> ...
> value_type move_front()
> {
> value_type v;
> pop(v);
> return v;
> }
>
> void pop(value_type& v)
> {
> v = std::move(c.front());
> c.pop_front();
> }
> };
>
I don't see the problem with stack and queue. stack top() returns a
reference and queue has no member top() and front() and back() returns a
reference.
Best,
Vicente