The unique_ptr
copy constructor is marked as deleted, i.e. the following
unique_ptr(const unique_ptr&) = delete;
And when you return a const unique_ptr<Req>
from your function, the return value is treated as an rvalue, and therefore the exact match for a constructor if available would be
unique_ptr(const unique_ptr&&);
The move constructor does not match const unique_ptr<Req>&&
because it requires a non const
rvalue reference. And so the closest match is the copy constructor which is deleted, therefore this does not work.
But the question you should be asking yourself is, why do you want to mark the unique_ptr
to be const in the first place? A const
unique_ptr
means that the pointer is const, and not the pointed to thing. If you want a unique_ptr
to const, you should do unique_ptr<const Req>
instead.