-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
해결됨
14분 35초 부근 질문 있습니다.
22.12.05 23:02 작성 조회수 203
1
auto doSomething2(std::unique_ptr<Resource> res)
{
res->setAll(100);
res->print();
return res;
}
여기서 res1 = doSomething2(std::move(res1));
이게 파라미터 res가 std::move(res1)을 인자로 받아서 r-value로 return되었기 때문에 가능하다고 이해했습니다.
auto& doSomething2(std::unique_ptr<Resource> res)
{
res->setAll(100);
res->print();
return res;
}
그래서 이렇게 해도 파라미터 res가 std::move(res1)인 r-value를 인자로 받아서 그대로 std::move(res1)을 return해서 res1 = doSomething2(std::move(res1));을 실행해줄 줄 알았는데 std::unique_ptr가 l-value로, copy semantics를 사용하는 함수를 delete해서 안된다고 나옵니다.
왜 std::move(res1)의 reference인데 r-value가 아니라 l-value로 return 되나요?
답변을 작성해보세요.
1
강민철
2022.12.08
저도 검색을 해서 알게 되었는데,
아래의 링크가 도움이 되지 않을까 싶습니다.
https://2pound2pound.tistory.com/82
https://stackoverflow.com/questions/57185454/why-does-operator-of-rvalue-unique-ptr-return-an-lvalue
답변 1