I am currently playing around with move semantics in C++. I tried to use r-value references in a custom constructor. Please see the following simplified example:
class MyClass
{
public:
// ....
MyClass(MyClass&& pLeft, int pOp, MyClass&& pRight);
MyClass(const MyClass& pLeft, int pOp, const MyClass& pRight);
// ....
};
MyClass operator +(MyClass&& pLeft, MyClass&& pRight)
{
return MyClass(pLeft, 1, pRight);
}
The operator+
is called as expected with pLeft
and pRight
being temporary objects. However then it calls the custom constructor using l-value references instead of the custom constructor using r-value references. Why?
There is a lot of information about r-value references and move semantics in the web. But it seems I skipped the detail that explains the mentioned behaviour.