I am trying to do something similar to this:
#include <vector>
#include <memory>
struct Bar
{
Bar& doThings()
{return *this;}
std::unique_ptr<int> m_content; // A non-copyable type
};
struct Foo
{
Foo& append(Bar&& obj)
{
objects.push_back(std::move(obj));
return *this;
}
std::vector<Bar> objects;
};
int test()
{
Foo test;
test.append(std::move(Bar{}.doThings())) //Ok
// Not ok
.append(Bar{}.doThings())
;
}
error: cannot bind rvalue reference of type
Bar&&
to lvalue of typeBar
Is it possible to make this work without the explicit std::move?
Trying to overload doThings does not solve the problem:
error:
Bar&& Bar::doThings() &&
cannot be overloaded
The problem is that when you return the instance from the function, you don't have an rvalue.
However, there is a way to overload functions based on the rvalue/lvalue of the object:
You can add ref-qualified overloads of
doThings()
: