Kla*_*aus 5 c++ c++17 structured-bindings
是否可以使用现有的变量作为与之相关的返回值的目标structured bindings?
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
}
Run Code Online (Sandbox Code Playgroud)
如果要使用现有变量,则可以std::tie实现此目的.
std::tie(x, z) = f(); // only works with tuples however
Run Code Online (Sandbox Code Playgroud)
结构化绑定引入了新标识符.不幸的是,没有什么能比得上std::tie一般的聚合.