***에 의해 주석 처리 된 행에서 Bar의 복사 생성자가 호출되는 이유는 무엇입니까? input_bar
은 rvalue 참조이므로 이동 생성자가 호출되기를 기대합니다. lvalue 참조로 변환 되었습니까? 해당 행을 bar_(std::move(input_bar))
으로 변경하면 이동 생성자 호출을 만들 수 있습니다.이동 생성자 구현
#include <iostream>
#include <array>
#include <memory>
class Bar
{
public:
Bar(const Bar& bar)
{
std::cout << "copy constructor called" << std::endl;
}
Bar(Bar&& bar)
{
std::cout << "move constructor called" << std::endl;
}
};
class Foo
{
public:
Foo(Bar&& input_bar) :
bar_(input_bar) // ***
{
}
Bar bar_;
};
int main()
{
Bar bar;
Foo foo(std::move(bar));
return 0;
}
참조 http://stackoverflow.com/q/27554296/981959 –