7

是否可以使用已经存在的变量作为返回值的目标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
}
4

1 回答 1

10

如果你想使用现有的变量,你有std::tie这个目的。

std::tie(x, z) = f(); // only works with tuples however

结构化绑定引入了新的标识符。不幸的是,没有什么等同std::tie于一般聚合。

于 2017-06-26T12:21:48.803 回答