无法与现有变量进行结构化绑定?
structured binding with existing vars not possible?
是否可以使用已经存在的变量作为与 structured bindings
相关的 return 值的目标?
auto f()
{
return std::make_tuple(1,2.2);
}
int main()
{
int x;
double z;
[ x, z ] = f(); // Did not work in gcc 7.1
// structured bindings only work with "new" vars?
auto [a, b] = f(); // works fine
}
如果您想使用现有变量,您有 std::tie
用于该目的。
std::tie(x, z) = f(); // only works with tuples however
结构化绑定引入了新的标识符。不幸的是,没有什么等同于 std::tie
的一般聚合。
是否可以使用已经存在的变量作为与 structured bindings
相关的 return 值的目标?
auto f()
{
return std::make_tuple(1,2.2);
}
int main()
{
int x;
double z;
[ x, z ] = f(); // Did not work in gcc 7.1
// structured bindings only work with "new" vars?
auto [a, b] = f(); // works fine
}
如果您想使用现有变量,您有 std::tie
用于该目的。
std::tie(x, z) = f(); // only works with tuples however
结构化绑定引入了新的标识符。不幸的是,没有什么等同于 std::tie
的一般聚合。