See code below. I comment move constructor and instead of compilation error, copy constructor is now called! Despite the fact that I am using std::move
.
How I can be ensure, that my huge object never call copy constructor (for example, I forgot to add move constructor) if I use std::move
?
class MyHugeObject
{
public:
MyHugeObject()
{
}
MyHugeObject(const MyHugeObject& m)
{
std::cout << "copy huge object\n";
}
// MyHugeObject(MyHugeObject&& m)
// {
// std::cout << "move huge object\n";
// }
};
MyHugeObject func()
{
MyHugeObject m1;
MyHugeObject&& m2 = std::move(m1);
return std::move(m2);
}
int main()
{
auto m = func();
return 0;
}
Output:
copy huge object