[英]Returning unique_ptr which is function parameter
我有以下方法:
std::unique_ptr<Req> RequestConverter::SetReg(
const std::unique_ptr<Req> pb_req, ...) {
我想從上面的方法返回參數pb_req。 我收到此錯誤(有或沒有std :: move):
error: call to deleted constructor of 'std::unique_ptr<Req>'
什么是推薦的方法?
謝謝
unique_ptr
復制構造函數標記為已刪除,即以下內容
unique_ptr(const unique_ptr&) = delete;
當您從函數返回const unique_ptr<Req>
,返回值將被視為右值,因此構造函數的完全匹配(如果可用)將是
unique_ptr(const unique_ptr&&);
移動構造函數與const unique_ptr<Req>&&
不匹配,因為它需要非const
rvalue引用。 因此最接近的匹配是被刪除的復制構造函數,因此這不起作用。
但是你應該問自己的問題是,為什么你想首先將unique_ptr
標記為const? const
unique_ptr
意味着指針是const,而不是指向的東西。 如果你想要一個unique_ptr
到const,你應該改為unique_ptr<const Req>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.