I'm implementing the Dependency Injection pattern in C++ with smart pointers. When using std::unique_ptr
I'm wondering if there is any difference passing pointers in the constructor by rvalue reference or by value?
Should I use:
class SomeClass
{
public:
SomeClass(std::unique_ptr<SomeInterface> someInterfaceInit) :
someInterface{std::move(someInterfaceInit)}
{}
private:
std::unique_ptr<SomeInterface> someInterface;
};
or should I use rvalue reference:
class SomeClass
{
public:
SomeClass(std::unique_ptr<SomeInterface>&& someInterfaceInit) :
someInterface{std::move(someInterfaceInit)}
{}
private:
std::unique_ptr<SomeInterface> someInterface;
};
And second question is why I cannot omit std::move
while using rvalue reference?