How does one exploit structured binding and tuples to return objects local to a function?
In a function, I am making local objects that reference each other, and I want to return these objects in a tuple and use structured binding to identify them whenever I call the function. I currently have this:
std::tuple<Owner&&, State<Controller>&&, State<Ancillary>&&, State<Compressor>&&>
inline makeOwner() {
State<Controller>&& controller = State<Controller>();
State<Ancillary>&& ancillary = State<Ancillary>();
State<Compressor>&& compressor = State<Compressor>();
Owner&& owner = Owner(controller, ancillary, compressor);
return {owner, controller, ancillary, compressor};
}
// using the function later
const &&[owner, controller, ancillary, compressor] = makeOwner();
This does not work, and I get an error saying the return value isn't convertable to a tuple of the aforementioned return type. I'm not sure why this is the case, since the types match up to the declarations.
Ultimately, I'm trying to create a convenience function so I don't have to type the four lines in the function every time I want to make a new Owner. This is my attempt at using structured binding to make this easier.
EDIT: I should note that I want the bindings in the last line to reference the objects inside of owner. So, copies are insufficient.