如何利用结构化绑定和元组将对象返回到函数的本地?
在一个函数中,我正在创建相互引用的本地对象,并且我想在一个元组中返回这些对象,并在我调用函数时使用结构化绑定来识别它们。我目前有这个:
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();
这不起作用,我收到一条错误消息,指出返回值不能转换为上述返回类型的元组。我不确定为什么会这样,因为类型与声明相匹配。
最终,我正在尝试创建一个方便的函数,这样每次我想创建一个新的所有者时,我都不必在函数中键入四行。这是我尝试使用结构化绑定使这更容易。
编辑:我应该注意,我希望最后一行中的绑定引用所有者内部的对象。所以,副本是不够的。