강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

thd2tn님의 프로필 이미지
thd2tn

작성한 질문수

홍정모의 따라하며 배우는 C++

15.5 std::unique_ptr

14분 35초 부근 질문 있습니다.

해결된 질문

작성

·

349

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

thd2tn님의 프로필 이미지
thd2tn

작성한 질문수

질문하기