-1
error: call to implicitly-deleted copy of 'unique_ptr<Searchable>'                                                                      
unique_ptr<Searchable> current(std::move(searchSpace.top()));

Hello,

I am trying to figure out why this doesn't compile. Search space is priority queue and I am trying to move rvalue searchSpace.top() to new current object.

Strangely enough, If I change priority queue to deque :

        unique_ptr<Searchable> current(std::move(Q.front()));

This worked fine when Q is deque.

So why is the latter one working not the first one?

rest of the error message:

 copy constructor is implicitly deleted because 'unique_ptr<Searchable,
  std::__1::default_delete<Searchable> >' has a user-declared move
  constructor
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT  
zcahfg2
  • 861
  • 1
  • 12
  • 27
  • You are missing a [mcve]. There's no way for us to know what your code is unless you show it to us. – Justin Nov 01 '17 at 20:22
  • `priority_queue::top` returns a `const&` to the element – Praetorian Nov 01 '17 at 20:24
  • I think I supplied more than enough information for this question. I was asking a general question why a member of certain data structure can be moved while others cannot... – zcahfg2 Nov 01 '17 at 20:34

2 Answers2

1

std::priority_queue::top() only has a const overload returning a const reference. This cannot be moved from.

std::deque::front() on the other hand has const and non-const overloads returning equally qualified references. You seem to be using the non-const overload, the result of which can be moved from.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

According to documentation of std::priority_queue::top()

const_reference top() const;

it returns const reference, which by std::move would be converted to const T && . On another side const T && is not accepted by move ctor of std::unqiue_ptr, as it does not have such overload, it only accepts T && (without const) see documentation of std::unqiue_ptr ctors

On why const reference is accepted by std::move see here Why can we use std::move on a const object?

Slava
  • 43,454
  • 1
  • 47
  • 90