Skip to content

Instantly share code, notes, and snippets.

@fortheday
Last active January 31, 2018 08:58
Show Gist options
  • Save fortheday/e5d37380754edbb84c3704d3c848b9aa to your computer and use it in GitHub Desktop.
Save fortheday/e5d37380754edbb84c3704d3c848b9aa to your computer and use it in GitHub Desktop.
// 이동 생성자/연산자는 RVO 및 int x(int())의 버그를 고려하고 학습/테스트 해야 한다.
class CTest {
public:
CTest() { puts("ctor"); }
CTest(const CTest &rRight) { puts("ctor-c"); } // copy
CTest(CTest &&rrRight) { puts("ctor-m"); } // move
CTest & operator=(const CTest &rRight) { puts("assign"); return *this; } // copy (일반적으로 딥카피)
CTest & operator=(CTest &&rrRight) noexcept { puts("assign-m"); return *this; } // move
static CTest Create_NoRVO(const int n) { return n ? CTest() : CTest(); }
int x;
};
{
CTest test( CTest() ); // ctor (RVO), VS2017에서 함수포인터?같은거로 인식한다.
// 해결을 위해서는 생성자에서 int 인자를 받으면 됨.
}
{
CTest test(CTest::Create_NoRVO(1)); // (1)ctor, (2)ctor-m
}
{
CTest test1 = std::move(CTest()); // (1)ctor (2)ctor-m
}
{
CTest test2; // (0)ctor
test2 = std::move(CTest()); // (1)ctor (2)assign-m
}
{
CTest &&rrTest = CTest(); // (1)ctor
[](CTest &&rrTest) {}(CTest()); // (1)ctor
}
{
CTest test;
[](CTest &&rrTest) {}(test); // COMPILE ERROR, test는 lvalue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment